Skip to content

Commit 17f3a4f

Browse files
committed
Deploy preview for PR 1211 🛫
1 parent fe8c4f1 commit 17f3a4f

File tree

585 files changed

+1240
-1492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

585 files changed

+1240
-1492
lines changed

pr-preview/pr-1211/_sources/library/stdtypes.rst.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5215,6 +5215,11 @@ Note, the *elem* argument to the :meth:`~object.__contains__`,
52155215
:meth:`~set.discard` methods may be a set. To support searching for an equivalent
52165216
frozenset, a temporary one is created from *elem*.
52175217

5218+
.. seealso::
5219+
5220+
For detailed information on thread-safety guarantees for :class:`set`
5221+
objects, see :ref:`thread-safety-set`.
5222+
52185223

52195224
.. _typesmapping:
52205225

pr-preview/pr-1211/_sources/library/threadsafety.rst.txt

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,108 @@ thread, iterate over a copy:
342342
343343
Consider external synchronization when sharing :class:`dict` instances
344344
across threads.
345+
346+
347+
.. _thread-safety-set:
348+
349+
Thread safety for set objects
350+
==============================
351+
352+
The :func:`len` function is lock-free and :term:`atomic <atomic operation>`.
353+
354+
The following read operation is lock-free. It does not block concurrent
355+
modifications and may observe intermediate states from operations that
356+
hold the per-object lock:
357+
358+
.. code-block::
359+
:class: good
360+
361+
elem in s # set.__contains__
362+
363+
This operation may compare elements using :meth:`~object.__eq__`, which can
364+
execute arbitrary Python code. During such comparisons, the set may be
365+
modified by another thread. For built-in types like :class:`str`,
366+
:class:`int`, and :class:`float`, :meth:`!__eq__` does not release the
367+
underlying lock during comparisons and this is not a concern.
368+
369+
All other operations from here on hold the per-object lock.
370+
371+
Adding or removing a single element is safe to call from multiple threads
372+
and will not corrupt the set:
373+
374+
.. code-block::
375+
:class: good
376+
377+
s.add(elem) # add element
378+
s.remove(elem) # remove element, raise if missing
379+
s.discard(elem) # remove element if present
380+
s.pop() # remove and return arbitrary element
381+
382+
These operations also compare elements, so the same :meth:`~object.__eq__`
383+
considerations as above apply.
384+
385+
The :meth:`~set.copy` method returns a new object and holds the per-object lock
386+
for the duration so that it is always atomic.
387+
388+
The :meth:`~set.clear` method holds the lock for its duration. Other
389+
threads cannot observe elements being removed.
390+
391+
The following operations only accept :class:`set` or :class:`frozenset`
392+
as operands and always lock both objects:
393+
394+
.. code-block::
395+
:class: good
396+
397+
s |= other # other must be set/frozenset
398+
s &= other # other must be set/frozenset
399+
s -= other # other must be set/frozenset
400+
s ^= other # other must be set/frozenset
401+
s & other # other must be set/frozenset
402+
s | other # other must be set/frozenset
403+
s - other # other must be set/frozenset
404+
s ^ other # other must be set/frozenset
405+
406+
:meth:`set.update`, :meth:`set.union`, :meth:`set.intersection` and
407+
:meth:`set.difference` can take multiple iterables as arguments. They all
408+
iterate through all the passed iterables and do the following:
409+
410+
* :meth:`set.update` and :meth:`set.union` lock both objects only when
411+
the other operand is a :class:`set`, :class:`frozenset`, or :class:`dict`.
412+
* :meth:`set.intersection` and :meth:`set.difference` always try to lock
413+
all objects.
414+
415+
:meth:`set.symmetric_difference` tries to lock both objects.
416+
417+
The update variants of the above methods also have some differences between
418+
them:
419+
420+
* :meth:`set.difference_update` and :meth:`set.intersection_update` try
421+
to lock all objects one-by-one.
422+
* :meth:`set.symmetric_difference_update` only locks the arguments if it is
423+
of type :class:`set`, :class:`frozenset`, or :class:`dict`.
424+
425+
The following methods always try to lock both objects:
426+
427+
.. code-block::
428+
:class: good
429+
430+
s.isdisjoint(other) # both locked
431+
s.issubset(other) # both locked
432+
s.issuperset(other) # both locked
433+
434+
Operations that involve multiple accesses, as well as iteration, are never
435+
atomic:
436+
437+
.. code-block::
438+
:class: bad
439+
440+
# NOT atomic: check-then-act
441+
if elem in s:
442+
s.remove(elem)
443+
444+
# NOT thread-safe: iteration while modifying
445+
for elem in s:
446+
process(elem) # another thread may modify s
447+
448+
Consider external synchronization when sharing :class:`set` instances
449+
across threads. See :ref:`freethreading-python-howto` for more information.

pr-preview/pr-1211/_sources/tutorial/errors.rst.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ A :keyword:`try` statement may have more than one *except clause*, to specify
121121
handlers for different exceptions. At most one handler will be executed.
122122
Handlers only handle exceptions that occur in the corresponding *try clause*,
123123
not in other handlers of the same :keyword:`!try` statement. An *except clause*
124-
may name multiple exceptions as a parenthesized tuple, for example::
124+
may name multiple exceptions, for example::
125125

126-
... except (RuntimeError, TypeError, NameError):
126+
... except RuntimeError, TypeError, NameError:
127127
... pass
128128

129129
A class in an :keyword:`except` clause matches exceptions which are instances of the

pr-preview/pr-1211/about.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ <h3>導航</h3>
356356
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
357357
<br>
358358
<br>
359-
最後更新於 3月 13, 2026 (00:24 UTC)。
359+
最後更新於 3月 13, 2026 (17:02 UTC)。
360360

361361
<a href="/bugs.html">發現 bug</a>
362362

pr-preview/pr-1211/bugs.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ <h2>說明文件的錯誤<a class="headerlink" href="#documentation-bugs" title=
250250
</section>
251251
<section id="getting-started-contributing-to-python-yourself">
252252
<span id="contributing-to-python"></span><h2>開始讓自己貢獻 Python<a class="headerlink" href="#getting-started-contributing-to-python-yourself" title="連結到這個標頭"></a></h2>
253-
<p>除了只是回報你所發現的錯誤之外,同樣也歡迎你提交修正它們的修補程式 (patch)。你可以在 <a class="reference external" href="https://devguide.python.org/">Python 開發者指南</a>中找到如何開始修補 Python 的更多資訊。如果你有任何問題,<a class="reference external" href="https://mail.python.org/mailman3/lists/core-mentorship.python.org/">核心導師郵寄清單</a>是一個友善的地方,你可以在那裡得到,關於 Python 修正錯誤的過程中,所有問題的答案。</p>
253+
<p>除了只是回報你所發現的錯誤之外,同樣也歡迎你提交修正它們的修補程式 (patch)。你可以在 <a class="reference external" href="https://mail.python.org/mailman3/lists/core-mentorship.python.org/">Python 開發者指南</a>中找到如何開始修補 Python 的更多資訊。如果你有任何問題,<a class="reference external" href="https://devguide.python.org/">核心導師郵寄清單</a>是一個友善的地方,你可以在那裡得到,關於 Python 修正錯誤的過程中,所有問題的答案。</p>
254254
</section>
255255
</section>
256256

@@ -393,7 +393,7 @@ <h3>導航</h3>
393393
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
394394
<br>
395395
<br>
396-
最後更新於 3月 13, 2026 (00:24 UTC)。
396+
最後更新於 3月 13, 2026 (17:02 UTC)。
397397

398398
<a href="/bugs.html">發現 bug</a>
399399

pr-preview/pr-1211/c-api/abstract.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ <h3>導航</h3>
365365
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
366366
<br>
367367
<br>
368-
最後更新於 3月 13, 2026 (00:24 UTC)。
368+
最後更新於 3月 13, 2026 (17:02 UTC)。
369369

370370
<a href="/bugs.html">發現 bug</a>
371371

pr-preview/pr-1211/c-api/allocation.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ <h3>導航</h3>
574574
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
575575
<br>
576576
<br>
577-
最後更新於 3月 13, 2026 (00:24 UTC)。
577+
最後更新於 3月 13, 2026 (17:02 UTC)。
578578

579579
<a href="/bugs.html">發現 bug</a>
580580

pr-preview/pr-1211/c-api/apiabiversion.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ <h3>導航</h3>
514514
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
515515
<br>
516516
<br>
517-
最後更新於 3月 13, 2026 (00:24 UTC)。
517+
最後更新於 3月 13, 2026 (17:02 UTC)。
518518

519519
<a href="/bugs.html">發現 bug</a>
520520

pr-preview/pr-1211/c-api/arg.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ <h3>導航</h3>
996996
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
997997
<br>
998998
<br>
999-
最後更新於 3月 13, 2026 (00:24 UTC)。
999+
最後更新於 3月 13, 2026 (17:02 UTC)。
10001000

10011001
<a href="/bugs.html">發現 bug</a>
10021002

pr-preview/pr-1211/c-api/bool.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ <h3>導航</h3>
376376
<a href="https://www.python.org/psf/donations/">敬請捐贈。</a>
377377
<br>
378378
<br>
379-
最後更新於 3月 13, 2026 (00:24 UTC)。
379+
最後更新於 3月 13, 2026 (17:02 UTC)。
380380

381381
<a href="/bugs.html">發現 bug</a>
382382

0 commit comments

Comments
 (0)