Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
胡斌
/
srs
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
winlin
2014-06-11 11:03:22 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
b7d8be46a135078368c56bd7ca59566f41b7c7f0
b7d8be46
1 parent
cccc483a
add research for usage for subprocess
隐藏空白字符变更
内嵌
并排对比
正在显示
2 个修改的文件
包含
86 行增加
和
0 行删除
trunk/research/python-subprocess/main.py
trunk/research/python-subprocess/python.subprocess.cpp
trunk/research/python-subprocess/main.py
0 → 100755
查看文件 @
b7d8be4
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
sys
,
shlex
,
time
,
subprocess
cmd
=
"./python.subprocess 0 800"
args
=
shlex
.
split
(
str
(
cmd
))
print
"cmd:
%
s, args:
%
s"
%
(
cmd
,
args
)
def
use_communicate
(
args
,
fout
,
ferr
):
process
=
subprocess
.
Popen
(
args
,
stdout
=
fout
,
stderr
=
ferr
)
(
stdout_str
,
stderr_str
)
=
process
.
communicate
()
return
(
stdout_str
,
stderr_str
)
def
use_poll
(
args
,
fout
,
ferr
,
timeout
):
(
stdout_str
,
stderr_str
)
=
(
None
,
None
)
process
=
subprocess
.
Popen
(
args
,
stdout
=
fout
,
stderr
=
ferr
)
starttime
=
time
.
time
()
while
True
:
process
.
poll
()
if
process
.
returncode
is
not
None
:
(
stdout_str
,
stderr_str
)
=
process
.
communicate
()
break
if
timeout
>
0
and
time
.
time
()
-
starttime
>=
timeout
:
print
"timeout, kill process. timeout=
%
s"
%
(
timeout
)
process
.
kill
()
break
time
.
sleep
(
1
)
process
.
wait
()
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"
)
fout
=
subprocess
.
PIPE
#fnull.fileno()
ferr
=
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_poll(args, fout, ferr, 10)
(
stdout_str
,
stderr_str
)
=
use_communicate_timeout
(
args
,
fout
,
ferr
,
10
)
def
print_result
(
stdout_str
,
stderr_str
):
if
stdout_str
is
None
:
stdout_str
=
""
if
stderr_str
is
None
:
stderr_str
=
""
print
"terminated, size of stdout=
%
s, stderr=
%
s"
%
(
len
(
stdout_str
),
len
(
stderr_str
))
while
True
:
time
.
sleep
(
1
)
print_result
(
stdout_str
,
stderr_str
)
...
...
trunk/research/python-subprocess/python.subprocess.cpp
0 → 100644
查看文件 @
b7d8be4
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
/**
# always to print to stdout and stderr.
g++ python.subprocess.cpp -o python.subprocess
*/
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<=
2
)
{
printf
(
"Usage: <%s> <interval_ms> <max_loop>
\n
"
" %s 50 100000
\n
"
,
argv
[
0
],
argv
[
0
]);
exit
(
-
1
);
return
-
1
;
}
int
interval_ms
=
::
atoi
(
argv
[
1
]);
int
max_loop
=
::
atoi
(
argv
[
2
]);
printf
(
"always to print to stdout and stderr.
\n
"
);
printf
(
"interval: %d ms
\n
"
,
interval_ms
);
printf
(
"max_loop: %d
\n
"
,
max_loop
);
for
(
int
i
=
0
;
i
<
max_loop
;
i
++
)
{
fprintf
(
stdout
,
"always to print to stdout and stderr. interval=%dms, max=%d, current=%d
\n
"
,
interval_ms
,
max_loop
,
i
);
fprintf
(
stderr
,
"always to print to stdout and stderr. interval=%dms, max=%d, current=%d
\n
"
,
interval_ms
,
max_loop
,
i
);
if
(
interval_ms
>
0
)
{
usleep
(
interval_ms
*
1000
);
}
}
return
0
;
}
...
...
请
注册
或
登录
后发表评论