Skip to content

Instantly share code, notes, and snippets.

@comynli
Last active December 26, 2015 12:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comynli/7148531 to your computer and use it in GitHub Desktop.
Save comynli/7148531 to your computer and use it in GitHub Desktop.
run cmd with non block_io
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