concurrent-iterator
Intro
Classes to run Producers (iterators) and Consumers (coroutines) introducing concurrency.
There are many libraries to create pipelines with stages running in separate processes, a nice one is parallelpipe, but this library does something different. It runs the entire upstream iterable (the Producer) in a background thread or process, while the downstream consumption code stays in the foreground. It's a more coarse library but easier to integrate since things keep looking like normal generators.
Installation
pip install concurrent-iterator
Implementations
There are three modules, each providing a Producer, a Consumer, and
(for thread) a MultiProducer:
dummy: non-concurrent implementation, useful for testing or as a drop-in replacement when concurrency is not needed.thread: uses a background thread to run the iterable or coroutine. Useful for IO-bound work.process: uses a background process to run the iterable or coroutine. Useful for CPU or IO-bound work. It has the complications of dealing with processes (different memory spaces, logging, etc). For logging, modulemultiprocessing-loggingcan be used.
All Producer, MultiProducer, and Consumer classes implement the
IProducer and IConsumer interfaces defined in concurrent_iterator
(the package root). They support:
- Iteration (
__next__/__iter__) — Producers yield values from the background iterable. - Sending (
Consumer.send) — Consumers forward values to the background coroutine. - Explicit close (
close()) — stops the background worker and releases resources. - State inspection (
closedproperty) — checks whether the worker has been closed. - Context manager (
withstatement) —close()is called automatically on exit.
Usage
Producer
import time
from concurrent_iterator.thread import Producer
def slow_generator(count):
for i in range(count):
time.sleep(0.5) # Simulate a slow producer
yield i
# Pre-calculate up to 5 values while the main thread consumes them.
with Producer(slow_generator(10), maxsize=5) as items:
for item in items:
print(item) # Do some time-consuming task
In this example, while the main thread processes each item,
slow_generator continues running in a background thread and
pre-calculates up to 5 values.
The with statement ensures the background thread is properly cleaned
up.
Consumer
from concurrent_iterator.thread import Consumer
def worker():
while True:
value = yield # Coroutine protocol
# ... do work with value ...
coro = worker()
next(coro) # Prime the coroutine
with Consumer(coro, maxsize=5) as consumer:
consumer.send("a")
consumer.send("b")
# close() is called automatically on exit, which also closes the coroutine.
The Consumer runs the coroutine in a background thread. send()
enqueues values; if the value cannot be enqueued within timeout
(default 0, never blocks), it raises
concurrent_iterator.WillNotConsume, signaling the caller to retry.
Context manager
Both Producers and Consumers can be used as context managers. When the
with block exits, close() is called automatically, ensuring
background threads or processes are terminated even on exceptions.
from concurrent_iterator.thread import Producer
with Producer(range(100), maxsize=10) as items:
for item in items:
if item > 5:
break # close() is still called by the with block
Exception handling
Exceptions raised by generators are forwarded to the main thread. When a
generator raises, the producer forwards the exception, re-raising it from
__next__(). After an exception, the producer is terminated and raises
StopIteration on subsequent calls.
For process.Producer, if the child process terminates unexpectedly,
__next__() raises RuntimeError("Child process died."), and becomes
closed.
Limitations
Limitations:
process.Producer/process.Consumerwith generators, coroutines and other unpicklable objects require theforkstart method. Python 3.14 changes the default on Linux fromforktoforkserver, andspawn/forkservercannot pickle generators (TypeError: cannot pickle 'generator' object). In those casesprocess.Producer/process.ConsumerraisesRuntimeErrorwith a clear message. Usethread.Producer/thread.Consumer, a picklable iterable (e.g.iter([1,2,3])), or passmp_context=multiprocessing.get_context('fork')whereforkis available (Linux).
Metadata
Release files for concurrent-iterator 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| concurrent_iterator-1.0.0.tar.gz | 17.8 kB | Without provenance | Details for concurrent_iterator-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| concurrent_iterator-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for concurrent_iterator-1.0.0-py3-none-any.whl |
Total release size: 32.8 kB
Release files / concurrent_iterator-1.0.0.tar.gz
| Download URL | concurrent_iterator-1.0.0.tar.gz |
|---|---|
| Size | 17.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73f993d2c0b2045080de0aa597cf685368f535270ca1a4a5c950d9b994bd7f1e
|
|
BLAKE2b-256 checksum How to use checksums |
2a1f53b4808aca60a14ebde57ee1195796c511be1f325dbd09a705b3c4579a37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.10
|
Release files / concurrent_iterator-1.0.0-py3-none-any.whl
| Download URL | concurrent_iterator-1.0.0-py3-none-any.whl |
|---|---|
| Size | 15.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
addd0c625b7fd9a5f9757b4c057578174e00c0eb948e5e3583d779ad851e0ba6
|
|
BLAKE2b-256 checksum How to use checksums |
56981d97b2eb580184eb48d3ae249c5576c44a6f1d899f430a69bf9ed3b4f780
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.10
|
Security for concurrent-iterator 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.