mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
finish the research for python-subprocess
This commit is contained in:
parent
b7d8be46a1
commit
17cb8c4550
1 changed files with 25 additions and 27 deletions
|
@ -1,17 +1,20 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import sys, shlex, time, subprocess
|
import sys, shlex, time, subprocess
|
||||||
|
|
||||||
cmd = "./python.subprocess 0 800"
|
cmd = "./python.subprocess 0 80000"
|
||||||
args = shlex.split(str(cmd))
|
args = shlex.split(str(cmd))
|
||||||
print "cmd: %s, args: %s"%(cmd, args)
|
print "cmd: %s, args: %s"%(cmd, args)
|
||||||
|
|
||||||
def use_communicate(args, fout, ferr):
|
# the communicate will read all data and wait for sub process to quit.
|
||||||
process = subprocess.Popen(args, stdout=fout, stderr=ferr)
|
def use_communicate(args, fout, ferr):
|
||||||
|
process = subprocess.Popen(args, stdout=fout, stderr=ferr)
|
||||||
(stdout_str, stderr_str) = process.communicate()
|
(stdout_str, stderr_str) = process.communicate()
|
||||||
return (stdout_str, stderr_str)
|
return (stdout_str, stderr_str)
|
||||||
|
|
||||||
|
# if use subprocess.PIPE, the pipe will full about 50KB data,
|
||||||
|
# and sub process will blocked, then timeout will kill it.
|
||||||
def use_poll(args, fout, ferr, timeout):
|
def use_poll(args, fout, ferr, timeout):
|
||||||
(stdout_str, stderr_str) = (None, None)
|
(stdout_str, stderr_str) = (None, None)
|
||||||
process = subprocess.Popen(args, stdout=fout, stderr=ferr)
|
process = subprocess.Popen(args, stdout=fout, stderr=ferr)
|
||||||
|
@ -28,27 +31,22 @@ def use_poll(args, fout, ferr, timeout):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
process.wait()
|
process.wait()
|
||||||
return (stdout_str, stderr_str)
|
return (stdout_str, stderr_str)
|
||||||
def use_communicate_timeout(args, fout, ferr, timeout):
|
|
||||||
(stdout_str, stderr_str) = (None, None)
|
|
||||||
process = subprocess.Popen(args, stdout=fout, stderr=ferr)
|
|
||||||
starttime = time.time()
|
|
||||||
while True:
|
|
||||||
|
|
||||||
fnull = open("/dev/null", "rw")
|
# stdout/stderr can be fd, fileobject, subprocess.PIPE, None
|
||||||
fout = subprocess.PIPE#fnull.fileno()
|
fnull = open("/dev/null", "rw")
|
||||||
ferr = subprocess.PIPE#fnull#fnull.fileno()
|
fout = fnull.fileno()#subprocess.PIPE#fnull#fnull.fileno()
|
||||||
print "fout=%s, ferr=%s"%(fout, ferr)
|
ferr = fnull.fileno()#subprocess.PIPE#fnull#fnull.fileno()
|
||||||
|
print "fout=%s, ferr=%s"%(fout, ferr)
|
||||||
#(stdout_str, stderr_str) = use_communicate(args, fout, ferr)
|
#(stdout_str, stderr_str) = use_communicate(args, fout, ferr)
|
||||||
#(stdout_str, stderr_str) = use_poll(args, fout, ferr, 10)
|
(stdout_str, stderr_str) = use_poll(args, fout, ferr, 10)
|
||||||
(stdout_str, stderr_str) = use_communicate_timeout(args, fout, ferr, 10)
|
|
||||||
|
|
||||||
def print_result(stdout_str, stderr_str):
|
def print_result(stdout_str, stderr_str):
|
||||||
if stdout_str is None:
|
if stdout_str is None:
|
||||||
stdout_str = ""
|
stdout_str = ""
|
||||||
if stderr_str is None:
|
if stderr_str is None:
|
||||||
stderr_str = ""
|
stderr_str = ""
|
||||||
print "terminated, size of stdout=%s, stderr=%s"%(len(stdout_str), len(stderr_str))
|
print "terminated, size of stdout=%s, stderr=%s"%(len(stdout_str), len(stderr_str))
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
print_result(stdout_str, stderr_str)
|
print_result(stdout_str, stderr_str)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue