Skip to content

Instantly share code, notes, and snippets.

@jaypei
Last active January 26, 2022 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jaypei/5263658 to your computer and use it in GitHub Desktop.
Save jaypei/5263658 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 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 ..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment