import asyncio import sys from asyncio.subprocess import PIPE CHILD_CMD = """import sys; sys.stdout.flush(); sys.stdout.buffer.write(b'x' * (2**17+1)); sys.stdout.flush()""" @asyncio.coroutine def fail(): child = yield from asyncio.create_subprocess_exec( sys.executable, '-c', CHILD_CMD, stdout=PIPE) total = 0 while True: chunk = yield from child.stdout.read(2048) if not chunk: break total += len(chunk) yield from asyncio.sleep(0.001) if (yield from child.wait()) != 0: raise ValueError() return total loop = asyncio.get_event_loop() try: print('read {} bytes from child'.format(loop.run_until_complete(fail()))) finally: loop.close()