Skip to content

Instantly share code, notes, and snippets.

@amirouche
Created August 15, 2015 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirouche/377ba1c5b6709c2de38a to your computer and use it in GitHub Desktop.
Save amirouche/377ba1c5b6709c2de38a to your computer and use it in GitHub Desktop.
Create a pipeline of (bash) commands using subprocess and trollius (or asyncio)
import trollius
from trollius import From
# from trollius import Return
from trollius import gather
from trollius import create_subprocess_shell
# create dummy bash programs
def bash_program(name, sleep, code):
with open(name, 'w') as f:
f.write("""sleep %s
echo %s finished
exit %s""" % (sleep, name, code))
return 'sh ' + name
flash = bash_program("flash.sh", 3, 10)
rapide = bash_program("rapide.sh", 5, 11)
lent = bash_program("lent.sh", 10, 21)
@trollius.coroutine
def join(*processus):
"""Wait that all `processus` are finished before returning"""
# https://docs.python.org/3/library/asyncio-task.html#asyncio.gather
yield From(gather(*map(lambda x: x.wait(), processus)))
@trollius.coroutine
def command_tree():
# Le global est necessaire dans le cadre de l'exemple "self-contained"
global flash, rapide, lent
# Utilisation de `create_subprocess_shell` api haut niveau de trollius
# d'autre façon d'interagir avec le processus existent, avec stdout
# stdin ou a travers des signaux
#
# https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.create_subprocess_shell
#
# Voir aussi:
#
# https://docs.python.org/3/library/asyncio-subprocess.html#subprocess-examples
#
# Et:
#
# https://docs.python.org/3/library/asyncio-subprocess.html#process
c = yield From(create_subprocess_shell(lent))
b = yield From(create_subprocess_shell(rapide))
a = yield From(create_subprocess_shell(flash))
# wait for all programs to be finished
yield join(a, b, c)
print 'voila le mot de passe:', a.returncode + b.returncode + c.returncode
loop = trollius.get_event_loop()
loop.run_until_complete(command_tree())
@amirouche
Copy link
Author

maybe replace L51 yield with yield from

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment