Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/multiprocessing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,12 @@ object -- see :ref:`multiprocessing-managers`.
Behavior is the same as in :meth:`threading.Lock.release` except that
when invoked on an unlocked lock, a :exc:`ValueError` is raised.

.. method:: locked()

Return true if the lock is acquired.

.. versionadded:: 3.10


.. class:: RLock()

Expand Down
3 changes: 3 additions & 0 deletions Lib/multiprocessing/synchronize.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ class Lock(SemLock):
def __init__(self, *, ctx):
SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)

def locked(self):
return self._semlock._is_zero()

def __repr__(self):
try:
if self._semlock._is_mine():
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,8 +1230,10 @@ class _TestLock(BaseTestCase):
def test_lock(self):
lock = self.Lock()
self.assertEqual(lock.acquire(), True)
self.assertTrue(lock.locked())
self.assertEqual(lock.acquire(False), False)
self.assertEqual(lock.release(), None)
self.assertFalse(lock.locked())
self.assertRaises((ValueError, threading.ThreadError), lock.release)

def test_rlock(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`multiprocessing.Lock` now has a `locked()` method that returns
whether the lock is currently held. Patch contributed by Rémi Lapeyre.