Skip to content

Instantly share code, notes, and snippets.

@OptimusT
Forked from jaypei/subprocess-bufsize-test.py
Created August 12, 2021 15:32
Show Gist options
  • Save OptimusT/bba73b6177eff58b15156c6f602f14df to your computer and use it in GitHub Desktop.
Save OptimusT/bba73b6177eff58b15156c6f602f14df 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