正在显示
1 个修改的文件
包含
8 行增加
和
10 行删除
| @@ -3,15 +3,18 @@ | @@ -3,15 +3,18 @@ | ||
| 3 | 3 | ||
| 4 | import sys, shlex, time, subprocess | 4 | import sys, shlex, time, subprocess |
| 5 | 5 | ||
| 6 | -cmd = "./python.subprocess 0 800" | 6 | +cmd = "./python.subprocess 0 80000" |
| 7 | args = shlex.split(str(cmd)) | 7 | args = shlex.split(str(cmd)) |
| 8 | print "cmd: %s, args: %s"%(cmd, args) | 8 | print "cmd: %s, args: %s"%(cmd, args) |
| 9 | 9 | ||
| 10 | +# the communicate will read all data and wait for sub process to quit. | ||
| 10 | def use_communicate(args, fout, ferr): | 11 | def use_communicate(args, fout, ferr): |
| 11 | process = subprocess.Popen(args, stdout=fout, stderr=ferr) | 12 | process = subprocess.Popen(args, stdout=fout, stderr=ferr) |
| 12 | (stdout_str, stderr_str) = process.communicate() | 13 | (stdout_str, stderr_str) = process.communicate() |
| 13 | return (stdout_str, stderr_str) | 14 | return (stdout_str, stderr_str) |
| 14 | 15 | ||
| 16 | +# if use subprocess.PIPE, the pipe will full about 50KB data, | ||
| 17 | +# and sub process will blocked, then timeout will kill it. | ||
| 15 | def use_poll(args, fout, ferr, timeout): | 18 | def use_poll(args, fout, ferr, timeout): |
| 16 | (stdout_str, stderr_str) = (None, None) | 19 | (stdout_str, stderr_str) = (None, None) |
| 17 | process = subprocess.Popen(args, stdout=fout, stderr=ferr) | 20 | process = subprocess.Popen(args, stdout=fout, stderr=ferr) |
| @@ -28,19 +31,14 @@ def use_poll(args, fout, ferr, timeout): | @@ -28,19 +31,14 @@ def use_poll(args, fout, ferr, timeout): | ||
| 28 | time.sleep(1) | 31 | time.sleep(1) |
| 29 | process.wait() | 32 | process.wait() |
| 30 | return (stdout_str, stderr_str) | 33 | return (stdout_str, stderr_str) |
| 31 | -def use_communicate_timeout(args, fout, ferr, timeout): | ||
| 32 | - (stdout_str, stderr_str) = (None, None) | ||
| 33 | - process = subprocess.Popen(args, stdout=fout, stderr=ferr) | ||
| 34 | - starttime = time.time() | ||
| 35 | - while True: | ||
| 36 | 34 | ||
| 35 | +# stdout/stderr can be fd, fileobject, subprocess.PIPE, None | ||
| 37 | fnull = open("/dev/null", "rw") | 36 | fnull = open("/dev/null", "rw") |
| 38 | -fout = subprocess.PIPE#fnull.fileno() | ||
| 39 | -ferr = subprocess.PIPE#fnull#fnull.fileno() | 37 | +fout = fnull.fileno()#subprocess.PIPE#fnull#fnull.fileno() |
| 38 | +ferr = fnull.fileno()#subprocess.PIPE#fnull#fnull.fileno() | ||
| 40 | print "fout=%s, ferr=%s"%(fout, ferr) | 39 | print "fout=%s, ferr=%s"%(fout, ferr) |
| 41 | #(stdout_str, stderr_str) = use_communicate(args, fout, ferr) | 40 | #(stdout_str, stderr_str) = use_communicate(args, fout, ferr) |
| 42 | -#(stdout_str, stderr_str) = use_poll(args, fout, ferr, 10) | ||
| 43 | -(stdout_str, stderr_str) = use_communicate_timeout(args, fout, ferr, 10) | 41 | +(stdout_str, stderr_str) = use_poll(args, fout, ferr, 10) |
| 44 | 42 | ||
| 45 | def print_result(stdout_str, stderr_str): | 43 | def print_result(stdout_str, stderr_str): |
| 46 | if stdout_str is None: | 44 | if stdout_str is None: |
-
请 注册 或 登录 后发表评论