Add multiprocessing.Lock.locked()
This commit is contained in:
parent
e005ead49b
commit
c0a2e2161d
|
@ -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()
|
||||
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue