I am trying to write and read to a stream without loading everything into memory at once. Here's what I would imagine working:
import io
stream = io.BytesIO()
def process_stream(stream):
while True:
chunk = stream.read(5).decode('utf-8')
if not chunk:
return
yield chunk
# this would be a separate thread, but here we just do it in serial:
for i in range(3):
stream.write(b'asdf')
for chunk in process_stream(stream):
print('I read', chunk)
But this actually doesn't print out anything. I can get it working, but only with the following two changes, either of which requires that all the bytes are held in memory at once:
- initializing
stream = io.BytesIO(b'asdf' * 3)
instead of incrementally writing - using
stream.getvalue()
instead of incrementally reading
I'm quite baffled that incremental writing can only be read by batch reading, and that incremental reading only works for batch writing. How can a get a constant-memory (assuming process_stream
outpaces writing) solution working?