Last active
August 29, 2015 14:10
-
-
Save kwarunek/3d86d1392a1236a2e9e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
import tornado.web | |
import subprocess | |
from tornado import gen | |
@tornado.gen.coroutine | |
def test(): | |
cmd = "echo 'test' | grep test" | |
data = yield gen.Task(_run, cmd) | |
print(data) | |
tornado.ioloop.IOLoop.instance().stop() | |
def _run(cmd, callback): | |
ioloop = tornado.ioloop.IOLoop.instance() | |
PIPE = subprocess.PIPE | |
pipe = subprocess.Popen(cmd , shell=True, stdin=PIPE, stdout=PIPE, stderr=subprocess.STDOUT, close_fds=True) | |
fd = pipe.stdout.fileno() | |
stdout = [] | |
def recv(*args): | |
data = pipe.stdout.readline() | |
if data: | |
stdout.append(data) | |
elif pipe.poll() is not None: | |
ioloop.remove_handler(fd) | |
callback(stdout) | |
ioloop.add_handler(fd, recv, ioloop.READ) | |
if __name__ == "__main__": | |
ioloop = tornado.ioloop.IOLoop.instance() | |
ioloop.add_callback(test) | |
ioloop.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment