Skip to content

Instantly share code, notes, and snippets.

@johnsca
Last active May 25, 2017 22:31
Show Gist options
  • Save johnsca/3bdf258fd6483e12f62cef36e2e49d07 to your computer and use it in GitHub Desktop.
Save johnsca/3bdf258fd6483e12f62cef36e2e49d07 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()
@johnsca
Copy link
Author

johnsca commented May 25, 2017

Not working as I expected. Only getting one line:

Starting subprocess
Tailing file
Line: line 1

Awating subprocess
Closing

@johnsca
Copy link
Author

johnsca commented May 25, 2017

Fixed. Correct output:

Starting subprocess
Tailing file
Line: line 1

Line: line 2

Line: line 3

Line: line 4

Line: line 5

Awating subprocess
Closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment