Skip to content

Commit fcb9ee8

Browse files
committed
Tweak wording. Add doctest.
1 parent d9dde84 commit fcb9ee8

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

Doc/library/threading.rst

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,9 +1461,11 @@ of the derived iterators.
14611461
.. class:: serialize(iterable)
14621462

14631463
Return an iterator wrapper that serializes concurrent calls to
1464-
:meth:`~iterator.__next__` using a lock. For generators, will also
1465-
serialize calls to :meth:`~generator.send`, :meth:`~generator.throw`,
1466-
and :meth:`~generator.close`.
1464+
:meth:`~iterator.__next__` using a lock.
1465+
1466+
If the wrapped iterator also defines :meth:`~generator.send`,
1467+
:meth:`~generator.throw`, or :meth:`~generator.close`, those calls
1468+
are serialized as well.
14671469

14681470
This makes it possible to share a single iterator, including a generator
14691471
iterator, between multiple threads. A lock assures that calls are handled
@@ -1474,7 +1476,9 @@ of the derived iterators.
14741476
:func:`next` while another thread is already advancing the iterator will
14751477
block until the active call completes.
14761478

1477-
Example::
1479+
Example:
1480+
1481+
.. doctest::
14781482

14791483
import threading
14801484

@@ -1505,7 +1509,9 @@ of the derived iterators.
15051509
This is especially useful as a :term:`decorator` for generator functions,
15061510
allowing their generator-iterators to be consumed from multiple threads.
15071511

1508-
Example::
1512+
Example:
1513+
1514+
.. doctest::
15091515

15101516
import threading
15111517

@@ -1519,7 +1525,7 @@ of the derived iterators.
15191525
it = counter()
15201526

15211527
def worker():
1522-
for _ in range(3):
1528+
for _ in range(5):
15231529
print(next(it))
15241530

15251531
threads = [threading.Thread(target=worker) for _ in range(2)]
@@ -1552,7 +1558,9 @@ of the derived iterators.
15521558
If *n* is ``0``, return an empty tuple. If *n* is negative, raise
15531559
:exc:`ValueError`.
15541560

1555-
Example::
1561+
Example:
1562+
1563+
.. doctest::
15561564

15571565
import threading
15581566

Lib/threading.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,10 @@ class BrokenBarrierError(RuntimeError):
849849
class serialize:
850850
"""Wrap a non-concurrent iterator with a lock to enforce sequential access.
851851
852-
Applies a non-reentrant lock around calls to __next__, send, throw, and close.
852+
Applies a non-reentrant lock around calls to __next__. If the
853+
wrapped iterator also defines send(), throw(), or close(), those
854+
calls are serialized as well.
855+
853856
Allows iterator and generator instances to be shared by multiple consumer
854857
threads.
855858
"""
@@ -906,10 +909,12 @@ def synchronized(func):
906909
Can also be used as a decorator for generator functions definitions
907910
so that the generator instances are serialized::
908911
912+
import time
913+
909914
@synchronized
910915
def enumerate_and_timestamp(iterable):
911916
for count, value in enumerate(iterable):
912-
yield count, time_ns(), value
917+
yield count, time.time_ns(), value
913918
914919
"""
915920

@@ -934,7 +939,7 @@ def concurrent_tee(iterable, n=2):
934939
"""
935940

936941
if n < 0:
937-
raise ValueError("n must be positive integer")
942+
raise ValueError("n must be a non-negative integer")
938943
if n == 0:
939944
return ()
940945
iterator = _concurrent_tee(iterable)

0 commit comments

Comments
 (0)