Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-115942: Addlocked
to several multiprocessing locks#115944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
dde2ca2
gh-115942: Add `locked` to several multiprocessing locks
sobolevn646c650
Update Modules/_threadmodule.c
sobolevna7e526a
merge
sobolevn7f548e1
Ready to be reviewed
sobolevn5b21db5
Add more tests
sobolevn6038ee6
More tests
sobolevne77a8ee
Address wording review
sobolevne72111b
Address review
sobolevn98ee84b
Add `_ModuleLock.locked`
sobolevn1a50a20
Apply suggestions from code review
sobolevnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletionsDoc/library/threading.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -382,6 +382,9 @@ def release(self): | ||
self.waiters.pop() | ||
self.wakeup.release() | ||
def locked(self): | ||
return bool(self.count) | ||
gpshead marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def __repr__(self): | ||
return f'_ModuleLock({self.name!r}) at {id(self)}' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1486,8 +1486,10 @@ def test_repr_lock(self): | ||
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) | ||
@staticmethod | ||
@@ -1549,16 +1551,23 @@ def test_repr_rlock(self): | ||
def test_rlock(self): | ||
lock = self.RLock() | ||
self.assertEqual(lock.acquire(), True) | ||
self.assertTrue(lock.locked()) | ||
self.assertEqual(lock.acquire(), True) | ||
self.assertEqual(lock.acquire(), True) | ||
self.assertEqual(lock.release(), None) | ||
self.assertTrue(lock.locked()) | ||
self.assertEqual(lock.release(), None) | ||
self.assertEqual(lock.release(), None) | ||
self.assertFalse(lock.locked()) | ||
self.assertRaises((AssertionError, RuntimeError), lock.release) | ||
def test_lock_context(self): | ||
with self.Lock() as locked: | ||
self.assertTrue(locked) | ||
sobolevn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def test_rlock_context(self): | ||
with self.RLock() as locked: | ||
self.assertTrue(locked) | ||
class _TestSemaphore(BaseTestCase): | ||
@@ -6254,6 +6263,7 @@ def test_event(self): | ||
@classmethod | ||
def _test_lock(cls, obj): | ||
obj.acquire() | ||
obj.locked() | ||
def test_lock(self, lname="Lock"): | ||
o = getattr(self.manager, lname)() | ||
@@ -6265,8 +6275,9 @@ def test_lock(self, lname="Lock"): | ||
def _test_rlock(cls, obj): | ||
obj.acquire() | ||
obj.release() | ||
obj.locked() | ||
def test_rlock(self, lname="RLock"): | ||
o = getattr(self.manager, lname)() | ||
self.run_worker(self._test_rlock, o) | ||
12 changes: 12 additions & 0 deletionsLib/test/lock_tests.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletionLib/threading.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletionsMisc/NEWS.d/next/Library/2025-04-01-11-16-22.gh-issue-115942.4W3hNx.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Add :meth:`threading.RLock.locked`, | ||
:meth:`multiprocessing.Lock.locked`, | ||
:meth:`multiprocessing.RLock.locked`, | ||
and allow :meth:`multiprocessing.managers.SyncManager.Lock` and | ||
:meth:`multiprocessing.managers.SyncManager.RLock` to proxy ``locked()`` call. |
15 changes: 15 additions & 0 deletionsModules/_threadmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.