PyDocs

Nav

Concurrent Execution

synchronized_iterator

threading

function · Added in 3.15 (source) not WASI
  • synchronized_iterator(func)
Wrap an iterator-returning callable to make its iterators thread-safe.

Existing itertools and more-itertools can be wrapped so that their
iterator instances are serialized.

For example, itertools.count does not make thread-safe instances,
but that is easily fixed with:

    atomic_counter = synchronized_iterator(itertools.count)

Can also be used as a decorator for generator function definitions
so that the generator instances are serialized::

    import time

    @synchronized_iterator
    def enumerate_and_timestamp(iterable):
        for count, value in enumerate(iterable):
            yield count, time.time_ns(), value
0 Examples
No examples for threading/synchronized_iterator.
See Also
No see-alsos for threading/synchronized_iterator
0 Notes
No notes for synchronized_iterator