-
-
Save adam-stokes/579f56f4bc07eeeade8e7c7532c4bc19 to your computer and use it in GitHub Desktop.
proof-of-concept async subprocess file tail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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