-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
gh-142518: Document thread-safety guarantees of set objects #145225
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
lysnikolaou
wants to merge
1
commit into
python:main
Choose a base branch
from
lysnikolaou:set-thread-safety
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.
+115
−0
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -260,3 +260,113 @@ thread, iterate over a copy: | |
|
|
||
| Consider external synchronization when sharing :class:`dict` instances | ||
| across threads. | ||
|
|
||
|
|
||
| .. _thread-safety-set: | ||
|
|
||
| Thread safety for set objects | ||
| ============================== | ||
|
|
||
| The :func:`len` function is lock-free and :term:`atomic <atomic operation>`. | ||
|
|
||
| The following read operation is lock-free. It does not block concurrent | ||
| modifications and may observe intermediate states from operations that | ||
| hold the per-object lock: | ||
|
|
||
| .. code-block:: | ||
| :class: good | ||
|
|
||
| elem in s # set.__contains__ | ||
|
|
||
| This operation may compare elements using :meth:`~object.__eq__`, which can | ||
| execute arbitrary Python code. During such comparisons, the set may be | ||
| modified by another thread. For built-in types like :class:`str`, | ||
| :class:`int`, and :class:`float`, :meth:`!__eq__` does not release the | ||
| underlying lock during comparisons and this is not a concern. | ||
|
|
||
| All other operations from here on hold the per-object lock. | ||
|
|
||
| Adding or removing a single element is safe to call from multiple threads | ||
| and will not corrupt the set: | ||
|
|
||
| .. code-block:: | ||
| :class: good | ||
|
|
||
| s.add(elem) # add element | ||
| s.remove(elem) # remove element, raise if missing | ||
| s.discard(elem) # remove element if present | ||
| s.pop() # remove and return arbitrary element | ||
|
|
||
| These operations also compare elements, so the same :meth:`~object.__eq__` | ||
| considerations as above apply. | ||
|
|
||
| The following operations return new objects and hold the per-object lock | ||
| for the duration: | ||
|
|
||
| .. code-block:: | ||
| :class: good | ||
|
|
||
| s.copy() # returns a shallow copy | ||
|
|
||
| The :meth:`~set.clear` method holds the lock for its duration. Other | ||
| threads cannot observe elements being removed. | ||
|
|
||
| The following operations only accept :class:`set` or :class:`frozenset` | ||
| as operands and always lock both objects: | ||
|
|
||
| .. code-block:: | ||
| :class: good | ||
|
|
||
| s |= other # other must be set/frozenset | ||
| s &= other # other must be set/frozenset | ||
| s -= other # other must be set/frozenset | ||
| s ^= other # other must be set/frozenset | ||
| s & other # other must be set/frozenset | ||
| s | other # other must be set/frozenset | ||
| s - other # other must be set/frozenset | ||
| s ^ other # other must be set/frozenset | ||
|
|
||
| :meth:`set.update`, :meth:`set.union`, :meth:`set.intersection` and | ||
| :meth:`set.difference` can take multiple iterables as arguments. They all | ||
| iterate through all the passed iterables and do the following: | ||
|
|
||
| * :meth:`set.update` and :meth:`set.union` lock both objects only when | ||
| the other operand is a :class:`set`, :class:`frozenset`, or :class:`dict`. | ||
| * :meth:`set.intersection` and :meth:`set.difference` always try to lock | ||
| all objects. | ||
|
|
||
| :meth:`set.symmetric_difference` tries to lock both objects. | ||
|
|
||
| The update variants of the above methods also have some differences between | ||
| them: | ||
|
|
||
| * :meth:`set.difference_update` and :meth:`set.intersection_update` try | ||
|
Contributor
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. It looks confusing as what objects are locked at a time. It would good to clarify if objects are locked one-by-one or at once. |
||
| to lock all objects. | ||
| * :meth:`set.symmetric_difference_update` only lock the argument if it is | ||
| of type :class:`set`, :class:`frozenset`, or :class:`dict`. | ||
|
|
||
| The following methods always try to lock both objects: | ||
|
|
||
| .. code-block:: | ||
| :class: good | ||
|
|
||
| s.isdisjoint(other) # both locked | ||
| s.issubset(other) # both locked | ||
| s.issuperset(other) # both locked | ||
|
|
||
| Operations that involve multiple accesses, as well as iteration, are never | ||
| atomic: | ||
|
|
||
| .. code-block:: | ||
| :class: bad | ||
|
|
||
| # NOT atomic: check-then-act | ||
| if elem in s: | ||
| s.remove(elem) | ||
|
|
||
| # NOT thread-safe: iteration while modifying | ||
| for elem in s: | ||
| process(elem) # another thread may modify s | ||
|
|
||
| Consider external synchronization when sharing :class:`set` instances | ||
| across threads. See :ref:`freethreading-python-howto` for more information. | ||
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.
it would be good to mention that
set.copyis always atomic