Skip to content

Instantly share code, notes, and snippets.

@JobsDong
Forked from FZambia/sub.py
Last active May 4, 2018 07:55
Show Gist options
  • Save JobsDong/6723392 to your computer and use it in GitHub Desktop.
Save JobsDong/6723392 to your computer and use it in GitHub Desktop.
使用tornado将命令行操作包装成异步
#!/usr/bin/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 -a")
print "result:", result
print "error:", error
IOLoop.instance().stop()
if __name__ == "__main__":
IOLoop.instance().add_callback(example_main)
IOLoop.instance().start()
@murisimov
Copy link

Thank you kind sir!

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