正在显示
1 个修改的文件
包含
25 行增加
和
27 行删除
| 1 | #!/usr/bin/python | 1 | #!/usr/bin/python |
| 2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- |
| 3 | - | ||
| 4 | -import sys, shlex, time, subprocess | ||
| 5 | - | ||
| 6 | -cmd = "./python.subprocess 0 800" | ||
| 7 | -args = shlex.split(str(cmd)) | ||
| 8 | -print "cmd: %s, args: %s"%(cmd, args) | ||
| 9 | - | ||
| 10 | -def use_communicate(args, fout, ferr): | ||
| 11 | - process = subprocess.Popen(args, stdout=fout, stderr=ferr) | 3 | + |
| 4 | +import sys, shlex, time, subprocess | ||
| 5 | + | ||
| 6 | +cmd = "./python.subprocess 0 80000" | ||
| 7 | +args = shlex.split(str(cmd)) | ||
| 8 | +print "cmd: %s, args: %s"%(cmd, args) | ||
| 9 | + | ||
| 10 | +# the communicate will read all data and wait for sub process to quit. | ||
| 11 | +def use_communicate(args, fout, 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,27 +31,22 @@ def use_poll(args, fout, ferr, timeout): | @@ -28,27 +31,22 @@ 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 | ||
| 37 | -fnull = open("/dev/null", "rw") | ||
| 38 | -fout = subprocess.PIPE#fnull.fileno() | ||
| 39 | -ferr = subprocess.PIPE#fnull#fnull.fileno() | ||
| 40 | -print "fout=%s, ferr=%s"%(fout, ferr) | 35 | +# stdout/stderr can be fd, fileobject, subprocess.PIPE, None |
| 36 | +fnull = open("/dev/null", "rw") | ||
| 37 | +fout = fnull.fileno()#subprocess.PIPE#fnull#fnull.fileno() | ||
| 38 | +ferr = fnull.fileno()#subprocess.PIPE#fnull#fnull.fileno() | ||
| 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): | ||
| 46 | - if stdout_str is None: | ||
| 47 | - stdout_str = "" | ||
| 48 | - if stderr_str is None: | ||
| 49 | - stderr_str = "" | 43 | +def print_result(stdout_str, stderr_str): |
| 44 | + if stdout_str is None: | ||
| 45 | + stdout_str = "" | ||
| 46 | + if stderr_str is None: | ||
| 47 | + stderr_str = "" | ||
| 50 | print "terminated, size of stdout=%s, stderr=%s"%(len(stdout_str), len(stderr_str)) | 48 | print "terminated, size of stdout=%s, stderr=%s"%(len(stdout_str), len(stderr_str)) |
| 51 | - while True: | ||
| 52 | - time.sleep(1) | 49 | + while True: |
| 50 | + time.sleep(1) | ||
| 53 | 51 | ||
| 54 | print_result(stdout_str, stderr_str) | 52 | print_result(stdout_str, stderr_str) |
-
请 注册 或 登录 后发表评论