-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-125828 : Fix 'get_value' missing on [Bounded]Semaphore on multiprocessing MacOSX #125829
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YvesDup
wants to merge
4
commits into
python:main
Choose a base branch
from
YvesDup:semaphore-macosx-multiprocessing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5d2997e
Initial commit
YvesDup 3c9dda3
Merge branch 'python:main' into semaphore-macosx-multiprocessing
YvesDup 5aeff96
Merge branch 'main' into semaphore-macosx-multiprocessing
YvesDup 171cc78
Merge branch 'python:main' into semaphore-macosx-multiprocessing
YvesDup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -125,21 +125,89 @@ def _make_name(): | |||||||||
return '%s-%s' % (process.current_process()._config['semprefix'], | ||||||||||
next(SemLock._rand)) | ||||||||||
|
||||||||||
if sys.platform == 'darwin': | ||||||||||
# | ||||||||||
# Specific MacOSX Semaphore | ||||||||||
# | ||||||||||
|
||||||||||
class _MacOSXSemaphore(SemLock): | ||||||||||
"""Dedicated class used only to workaround the missing | ||||||||||
function 'sem_getvalue', when interpreter runs on MacOSX. | ||||||||||
Add a shared counter for each [Bounded]Semaphore in order | ||||||||||
to handle internal counter when acquire and release operations | ||||||||||
are called. | ||||||||||
""" | ||||||||||
|
||||||||||
def __init__(self, kind, value, maxvalue, *, ctx): | ||||||||||
util.debug(f"_MacOSXSemaphore:: creation of a {self.__class__.__name__}"\ | ||||||||||
f"with '{value = }'") | ||||||||||
Comment on lines
+142
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
SemLock.__init__(self, kind, value, maxvalue, ctx=ctx) | ||||||||||
self._count = ctx.Value('L', value) # May be more than 'L' ? | ||||||||||
|
||||||||||
def _acquire(self, *args, **kwargs) -> bool: | ||||||||||
if self._semlock.acquire(*args, **kwargs): | ||||||||||
with self._count: | ||||||||||
util.debug(f"_MacOSXSemaphore: acquire {repr(self)}") | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
self._count.value -= 1 | ||||||||||
return True | ||||||||||
return False | ||||||||||
|
||||||||||
def _release(self): | ||||||||||
with self._count: | ||||||||||
self._count.value += 1 | ||||||||||
self._semlock.release() | ||||||||||
util.debug(f"_MacOSXSemaphore: release {repr(self)}") | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
def _release_bounded(self): | ||||||||||
if self._count.value + 1 > self._semlock.maxvalue: | ||||||||||
raise ValueError(f"Cannot exceed initial value of"\ | ||||||||||
f" {self._semlock.maxvalue!a}") | ||||||||||
self._release() | ||||||||||
|
||||||||||
def _get_value(self) -> int: | ||||||||||
return self._count.value | ||||||||||
|
||||||||||
def _make_methods(self): | ||||||||||
super()._make_methods() | ||||||||||
util.debug("_MacOSXSemaphore: _make_methods call") | ||||||||||
self.acquire = self._acquire | ||||||||||
if isinstance(self, BoundedSemaphore): | ||||||||||
self.release = self._release_bounded | ||||||||||
elif isinstance(self, Semaphore): | ||||||||||
self.release = self._release | ||||||||||
else: | ||||||||||
raise RuntimeError("Class dedicated only to Semaphore or BoundedSemaphore OSX") | ||||||||||
self.get_value = self._get_value | ||||||||||
|
||||||||||
def __setstate__(self, state): | ||||||||||
self._count, state = state[-1], state[:-1] | ||||||||||
super().__setstate__(state) | ||||||||||
|
||||||||||
def __getstate__(self) -> tuple: | ||||||||||
return super().__getstate__() + (self._count,) | ||||||||||
|
||||||||||
|
||||||||||
_SemClass = _MacOSXSemaphore | ||||||||||
else: | ||||||||||
_SemClass = SemLock | ||||||||||
|
||||||||||
# | ||||||||||
# Semaphore | ||||||||||
# | ||||||||||
|
||||||||||
class Semaphore(SemLock): | ||||||||||
class Semaphore(_SemClass): | ||||||||||
|
||||||||||
def __init__(self, value=1, *, ctx): | ||||||||||
SemLock.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX, ctx=ctx) | ||||||||||
_SemClass.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX, ctx=ctx) | ||||||||||
|
||||||||||
def get_value(self): | ||||||||||
"""redefined when MacOSX. | ||||||||||
""" | ||||||||||
return self._semlock._get_value() | ||||||||||
|
||||||||||
def __repr__(self): | ||||||||||
try: | ||||||||||
value = self._semlock._get_value() | ||||||||||
value = self.get_value() | ||||||||||
except Exception: | ||||||||||
value = 'unknown' | ||||||||||
return '<%s(value=%s)>' % (self.__class__.__name__, value) | ||||||||||
|
@@ -151,11 +219,11 @@ def __repr__(self): | |||||||||
class BoundedSemaphore(Semaphore): | ||||||||||
|
||||||||||
def __init__(self, value=1, *, ctx): | ||||||||||
SemLock.__init__(self, SEMAPHORE, value, value, ctx=ctx) | ||||||||||
_SemClass.__init__(self, SEMAPHORE, value, value, ctx=ctx) | ||||||||||
|
||||||||||
def __repr__(self): | ||||||||||
try: | ||||||||||
value = self._semlock._get_value() | ||||||||||
value = self.get_value() | ||||||||||
except Exception: | ||||||||||
value = 'unknown' | ||||||||||
return '<%s(value=%s, maxvalue=%s)>' % \ | ||||||||||
|
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to handle the SemLock class differently at the C level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned in the problem, I'm waiting for feedback (from the Mac OS team ?) before going any further.
A C implementation is an option, even if it seems more complicated than Python.
That said, I'm available to go deep into the SemLock C code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, since the SemLock class is implemented in C, it's preferrable to first patch it there IMO. But let's CC some people I know they are on macOS: @ned-deily @ronaldoussoren @freakboy3742.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't profess any particular expertise in this area. However, given the underlying implementation is written in C, I agree that it makes some sense for the implementation of this workaround to also be in C (rather than have an underlying implementation that returns NotImplemented, wrapped by a layer that overrides that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main idea is to listen to the
acquire
andrelease
methods to update a shared counter, and to return the counter value when theget_value
method is invoked.I am going to open a new PR with a workaround written in C.