Skip to content

Instantly share code, notes, and snippets.

@510908220
Forked from jaypei/subprocess-bufsize-test.py
Last active January 30, 2023 04:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 510908220/8171128c387e1abc6fb94b15f75f86f7 to your computer and use it in GitHub Desktop.
Save 510908220/8171128c387e1abc6fb94b15f75f86f7 to your computer and use it in GitHub Desktop.
使用subprocess。避免在子程序大量输出时因buffer size满,导致父进程hang住。
#!/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
#!/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=['root@182.92.67.22: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