Last active
December 26, 2015 12:08
-
-
Save comynli/7148531 to your computer and use it in GitHub Desktop.
run cmd with non block_io
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 os | |
import fcntl | |
import subprocess | |
def non_block_read(output): | |
fd = output.fileno() | |
fl = fcntl.fcntl(fd, fcntl.F_GETFL) | |
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) | |
try: | |
return output.read() | |
except EOFError: | |
raise EOFError | |
except Exception, e: | |
pass | |
def run_cmd(cmd): | |
try: | |
p = subprocess.Popen( | |
cmd, bufsize=10240, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, close_fds=True) | |
while True: | |
data = non_block_read(p.stdout) | |
print data | |
if p.poll() is not None: | |
break | |
except Exception, e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment