-
-
Save pcdinh/1743283 to your computer and use it in GitHub Desktop.
Asynchronous run command lazily yielding command output (Python gevent)
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
def run_seq(cmd): | |
"""Run `cmd` and yield its output lazily""" | |
p = subprocess.Popen( | |
cmd, shell=True, | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT) | |
# make STDIN and STDOUT non-blocking | |
fcntl.fcntl(p.stdin, fcntl.F_SETFL, os.O_NONBLOCK) | |
fcntl.fcntl(p.stdout, fcntl.F_SETFL, os.O_NONBLOCK) | |
p.stdin.close() | |
while True: | |
try: | |
chunk = p.stdout.read(32) | |
if not chunk: | |
break | |
yield chunk | |
except IOError: | |
ex = sys.exc_info()[1] | |
if ex[0] != errno.EAGAIN: | |
raise | |
sys.exc_clear() | |
socket.wait_read(p.stdout.fileno()) | |
p.stdout.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment