Skip to content

Instantly share code, notes, and snippets.

@adam-stokes
Forked from johnsca/c.py
Created May 25, 2017 22:31
Show Gist options
  • Save adam-stokes/579f56f4bc07eeeade8e7c7532c4bc19 to your computer and use it in GitHub Desktop.
Save adam-stokes/579f56f4bc07eeeade8e7c7532c4bc19 to your computer and use it in GitHub Desktop.
proof-of-concept async subprocess file tail
#!/usr/bin/python3
import asyncio
import aiofiles
import textwrap
consumer = textwrap.dedent(
"""
for i in {1..5}; do
echo line $i
sleep 1
done
""")
async def main():
async with aiofiles.open('p', 'w') as p:
await p.write(consumer)
async with aiofiles.open('o.txt', 'w') as o:
print('Starting subprocess')
proc = await asyncio.create_subprocess_exec('bash', './p', stdout=o)
print('Tailing file')
async with aiofiles.open('o.txt', 'r') as f:
while proc.returncode is None:
async for line in f:
print('Line: {}'.format(line))
print('Awating subprocess')
await proc.wait()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print('Closing')
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment