Does anyone know of anything equivalent to itertools.tee
but with the ability to dynamically add iterators?
The itertools.tee
function does exactly what I want, except that the number of iterators must be fixed when the function is called. I'd like something with equivalent functionality, but which permits new iterators to be added later, potentially even after some of the iterators have started iterating.
I'd like to avoid calling itertools.tee
multiple times, as this will potentially use a lot of memory (I could have millions of iterators). From the Python docs for itertools.tee
:
The following Python code helps explain what tee does (although the actual implementation is more complex and uses only a single underlying FIFO queue).
...
If new iterators are added later, they only need to see newly-arriving data.
Here's a code example of what I'd like to have (may not be syntactically correct):
input_iterator = iter([1, 2, 3, 4, 5, 6])
tee = EnhancedTee(input_iterator)
it1 = iter(tee)
val = it1.next()
# val == 1
it2 = iter(tee)
val = it2.next()
# val == 2
val = it1.next()
# val == 2