Skip to content

Instantly share code, notes, and snippets.

@egoag
Last active September 13, 2017 09:45
Show Gist options
  • Save egoag/e270183dd98ed90fd5627b2a3f3208ac to your computer and use it in GitHub Desktop.
Save egoag/e270183dd98ed90fd5627b2a3f3208ac to your computer and use it in GitHub Desktop.
Python asyncio subprocess
# *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