Skip to content

Commit 4406050

Browse files
committed
update docs
1 parent b675631 commit 4406050

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Doc/library/threading.rst

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,35 @@ This module defines the following functions:
146146

147147
.. function:: iter_locked(iterable)
148148

149-
Make an iterable thread-safe.
149+
Convert an iterable into an iterator that performs iteration using locks.
150150

151-
Roughly equivalent to::
151+
The ``iter_locked`` makes non-atomic iterators atomic::
152+
153+
class non_atomic_iterator:
154+
155+
def __init__(self, it):
156+
self.it = iter(it)
157+
158+
def __iter__(self):
159+
return self
160+
161+
def __next__(self):
162+
a = next(self.it)
163+
b = next(self.it)
164+
return a, b
165+
166+
atomic_iterator = iter_locked(non_atomic_iterator())
167+
168+
The ``iter_locked`` allows concurrent iteration over generator objects. For example::
169+
170+
def count():
171+
i = 0
172+
while True:
173+
i += 1
174+
yield i
175+
concurrent_iterator = iter_locked(count())
176+
177+
The implementation is roughly equivalent to::
152178

153179
class iter_locked(Iterator):
154180
def __init__(self, it):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add :meth:`threading.iter_locked` to make iterators thread-safe.
1+
Add :meth:`threading.iter_locked` to make concurrent iteration over an iterable execute using a lock.

0 commit comments

Comments
 (0)