-
-
Save johnsca/3bdf258fd6483e12f62cef36e2e49d07 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() |
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
Not working as I expected. Only getting one line: