Last active
September 13, 2017 09:45
-
-
Save egoag/e270183dd98ed90fd5627b2a3f3208ac to your computer and use it in GitHub Desktop.
Python asyncio subprocess
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
# *nix + python 3.6 | |
import asyncio | |
import subprocess | |
async def _run(cmd, **kwargs): | |
process = await asyncio.create_subprocess_shell( | |
cmd, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT, | |
**kwargs) | |
_ = input('Press return to load output(you can wait seconds).') | |
while True: | |
output = await process.stdout.readline() | |
if not output: | |
return | |
else: | |
print(output.decode()[:-1]) # drop '\n' | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(_run('ping -c 10 localhost')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment