Skip to content

Instantly share code, notes, and snippets.

@samuell
Last active December 2, 2015 12:36
Show Gist options
  • Save samuell/3db7640dd24abb0fd630 to your computer and use it in GitHub Desktop.
Save samuell/3db7640dd24abb0fd630 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3.5
# Adapted from J.F. Sebastian's answer at http://stackoverflow.com/a/34027086/340811
import asyncio
import sys
from asyncio.subprocess import PIPE, STDOUT
async def get_lines(shell_command):
p = await asyncio.create_subprocess_shell(shell_command, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
return (await p.communicate())[0].splitlines()
async def main():
# get commands output concurrently
coros = [get_lines('sleep {i:d}; echo {i:d}'.format(i=i)) for i in reversed(range(1000))]
for f in asyncio.as_completed(coros): # print in the order they finish
print(await f)
if sys.platform.startswith('win'):
loop = asyncio.ProactorEventLoop() # for subprocess' pipes on Windows
asyncio.set_event_loop(loop)
else:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment