Skip to content

Instantly share code, notes, and snippets.

@Nilom
Last active January 25, 2018 19:01
Show Gist options
  • Save Nilom/9106792 to your computer and use it in GitHub Desktop.
Save Nilom/9106792 to your computer and use it in GitHub Desktop.
Async subprocess for Tornado.
#!/usr/bin/env python2.7
#-*- coding=utf-8 -*-
import shlex
import subprocess
from tornado.gen import coroutine, Task, Return
from tornado.process import Subprocess
from tornado.ioloop import IOLoop
@coroutine
def call_subprocess(cmd, stdin_data=None, stdin_async=True):
"""call sub process async
Args:
cmd: str, commands
stdin_data: str, data for standard in
stdin_async: bool, whether use async for stdin
"""
stdin = Subprocess.STREAM if stdin_async else subprocess.PIPE
sub_process = Subprocess(shlex.split(cmd),
stdin=stdin,
stdout=Subprocess.STREAM,
stderr=Subprocess.STREAM,)
if stdin_data:
if stdin_async:
yield Task(sub_process.stdin.write, stdin_data)
else:
sub_process.stdin.write(stdin_data)
if stdin_async or stdin_data:
sub_process.stdin.close()
result, error = yield [Task(sub_process.stdout.read_until_close),
Task(sub_process.stderr.read_until_close),]
raise Return((result, error))
@coroutine
def example_main():
result, error = yield call_subprocess("ls")
result, error = yield call_subprocess("wc -l",stdin_data=result)
result, error = yield call_subprocess("ls -l |tail -2")
print result
IOLoop.instance().stop()
if __name__ == "__main__":
IOLoop.instance().add_callback(example_main)
IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment