-
-
Save 19317362/1d1ba252226ae750ce260a022ce27a9d to your computer and use it in GitHub Desktop.
使用subprocess。避免在子程序大量输出时因buffer size满,导致父进程hang住。
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import subprocess | |
from StringIO import StringIO | |
out = StringIO() | |
sp = subprocess.Popen(["python", "test_output.py"], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT) | |
while sp.returncode is None: | |
data = sp.stdout.read() | |
out.write(data) | |
sp.poll() | |
print out.getvalue() | |
#-------------------------- | |
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import subprocess | |
from StringIO import StringIO | |
import time | |
out = StringIO() | |
# sp = subprocess.Popen(["python", "test_output.py"], | |
# stdout=subprocess.PIPE, | |
# stderr=subprocess.STDOUT) | |
fab_cmd = ["fab" ,"-f", "test_output.py", "main"] | |
# fab -f test_output.py main | |
p = subprocess.Popen(fab_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
begin = time.time() | |
# p.wait() 826 | |
while True: | |
print "start.." | |
output = p.stdout.readline() | |
print "output:",output | |
print "end" | |
if output == '' and p.poll() is not None: | |
break | |
print "spnt:",time.time() - begin |
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import sys | |
for i in range(1000): | |
print "WARNING: hello jaypei from stdout ..." | |
for i in range(1000): | |
print >> sys.stderr, "WARNING: hello jaypei from stderr ..." | |
#------------------------ | |
from fabric.api import cd,run,env,hosts, task | |
env.hosts=['[email protected]:22'] | |
env.password='xxxxxxx' | |
def get_disk_usage(): | |
ret = run("ls /home") | |
return ret | |
def get_date(disk): | |
ret = run("ls /") | |
return ret | |
@task | |
def main(): | |
for i in range(300): | |
disk = get_disk_usage() | |
xx = get_date(disk) | |
print "out:",disk | |
# http://stackoverflow.com/questions/8077868/python-subprocess-popen-and-subprocess-call-hang | |
# http://www.cnblogs.com/icejoywoo/p/3627397.html | |
# http://www.macaronikazoo.com/?p=607 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment