File tree Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Expand file tree Collapse file tree 2 files changed +29
-3
lines changed Original file line number Diff line number Diff 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):
Original file line number Diff line number Diff line change 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 .
You can’t perform that action at this time.
0 commit comments