正在显示
2 个修改的文件
包含
86 行增加
和
0 行删除
trunk/research/python-subprocess/main.py
0 → 100755
| 1 | +#!/usr/bin/python | ||
| 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) | ||
| 12 | + (stdout_str, stderr_str) = process.communicate() | ||
| 13 | + return (stdout_str, stderr_str) | ||
| 14 | + | ||
| 15 | +def use_poll(args, fout, ferr, timeout): | ||
| 16 | + (stdout_str, stderr_str) = (None, None) | ||
| 17 | + process = subprocess.Popen(args, stdout=fout, stderr=ferr) | ||
| 18 | + starttime = time.time() | ||
| 19 | + while True: | ||
| 20 | + process.poll() | ||
| 21 | + if process.returncode is not None: | ||
| 22 | + (stdout_str, stderr_str) = process.communicate() | ||
| 23 | + break | ||
| 24 | + if timeout > 0 and time.time() - starttime >= timeout: | ||
| 25 | + print "timeout, kill process. timeout=%s"%(timeout) | ||
| 26 | + process.kill() | ||
| 27 | + break | ||
| 28 | + time.sleep(1) | ||
| 29 | + process.wait() | ||
| 30 | + 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 | + | ||
| 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) | ||
| 41 | +#(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) | ||
| 44 | + | ||
| 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 = "" | ||
| 50 | + print "terminated, size of stdout=%s, stderr=%s"%(len(stdout_str), len(stderr_str)) | ||
| 51 | + while True: | ||
| 52 | + time.sleep(1) | ||
| 53 | + | ||
| 54 | +print_result(stdout_str, stderr_str) |
| 1 | +#include <stdio.h> | ||
| 2 | +#include <unistd.h> | ||
| 3 | +#include <stdlib.h> | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | +# always to print to stdout and stderr. | ||
| 7 | +g++ python.subprocess.cpp -o python.subprocess | ||
| 8 | +*/ | ||
| 9 | +int main(int argc, char** argv) { | ||
| 10 | + if (argc <= 2) { | ||
| 11 | + printf("Usage: <%s> <interval_ms> <max_loop>\n" | ||
| 12 | + " %s 50 100000\n", argv[0], argv[0]); | ||
| 13 | + exit(-1); | ||
| 14 | + return -1; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + int interval_ms = ::atoi(argv[1]); | ||
| 18 | + int max_loop = ::atoi(argv[2]); | ||
| 19 | + printf("always to print to stdout and stderr.\n"); | ||
| 20 | + printf("interval: %d ms\n", interval_ms); | ||
| 21 | + printf("max_loop: %d\n", max_loop); | ||
| 22 | + | ||
| 23 | + for (int i = 0; i < max_loop; i++) { | ||
| 24 | + fprintf(stdout, "always to print to stdout and stderr. interval=%dms, max=%d, current=%d\n", interval_ms, max_loop, i); | ||
| 25 | + fprintf(stderr, "always to print to stdout and stderr. interval=%dms, max=%d, current=%d\n", interval_ms, max_loop, i); | ||
| 26 | + if (interval_ms > 0) { | ||
| 27 | + usleep(interval_ms * 1000); | ||
| 28 | + } | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + return 0; | ||
| 32 | +} |
-
请 注册 或 登录 后发表评论