Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
avocado
提交
d6c38093
A
avocado
项目概览
openeuler
/
avocado
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
avocado
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d6c38093
编写于
12月 11, 2018
作者:
C
Cleber Rosa
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'ldoktor/kill_process_tree3'
Signed-off-by:
N
Cleber Rosa
<
crosa@redhat.com
>
上级
966970d5
7b9868f0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
39 addition
and
20 deletion
+39
-20
avocado/utils/process.py
avocado/utils/process.py
+20
-14
selftests/unit/test_utils_process.py
selftests/unit/test_utils_process.py
+19
-6
未找到文件。
avocado/utils/process.py
浏览文件 @
d6c38093
...
...
@@ -209,36 +209,42 @@ def kill_process_tree(pid, sig=signal.SIGKILL, send_sigcont=True,
:param pid: The pid of the process to signal.
:param sig: The signal to send to the processes.
:param send_sigcont: Send SIGCONT to allow killing stopped processes
:param timeout: How long to wait for the pid(s) to die
(negative=infinity, 0=don't wait,
positive=number_of_seconds)
:return: list of all PIDs we sent signal to
:rtype: list
"""
if
timeout
<=
0
:
def
remaining_time
()
:
return
timeout
else
:
start
=
time
.
time
()
def
_all_pids_dead
(
killed_pids
)
:
for
pid
in
killed_pids
:
if
pid_exists
(
pid
):
return
False
return
True
def
remaining_time
()
:
return
timeout
-
time
.
time
()
+
start
if
timeout
>
0
:
start
=
time
.
time
()
if
not
safe_kill
(
pid
,
signal
.
SIGSTOP
):
return
return
[
pid
]
killed_pids
=
[
pid
]
for
child
in
get_children_pids
(
pid
):
kill
_process_tree
(
int
(
child
),
sig
,
timeout
=
remaining_time
(
))
kill
ed_pids
.
extend
(
kill_process_tree
(
int
(
child
),
sig
,
False
))
safe_kill
(
pid
,
sig
)
if
send_sigcont
:
safe_kill
(
pid
,
signal
.
SIGCONT
)
for
pid
in
killed_pids
:
safe_kill
(
pid
,
signal
.
SIGCONT
)
if
timeout
==
0
:
return
return
killed_pids
elif
timeout
>
0
:
if
not
wait_for
(
lambda
:
not
pid_exists
(
pid
),
remaining_
time
(),
step
=
0.01
):
if
not
wait_for
(
_all_pids_dead
,
timeout
+
start
-
time
.
time
(),
step
=
0.01
,
args
=
(
killed_pids
[::
-
1
],)
):
raise
RuntimeError
(
"Timeout reached when waiting for pid %s "
"and children to die (%s)"
%
(
pid
,
timeout
))
else
:
while
pid_exists
(
pid
):
while
not
_all_pids_dead
(
killed_pids
[::
-
1
]
):
time
.
sleep
(
0.01
)
return
killed_pids
def
kill_process_by_pattern
(
pattern
):
...
...
selftests/unit/test_utils_process.py
浏览文件 @
d6c38093
...
...
@@ -454,7 +454,7 @@ class MiscProcessTests(unittest.TestCase):
sleep
):
safe_kill
.
return_value
=
True
get_children_pids
.
return_value
=
[]
process
.
kill_process_tree
(
1
)
self
.
assertEqual
([
1
],
process
.
kill_process_tree
(
1
)
)
self
.
assertEqual
(
sleep
.
call_count
,
0
)
@
mock
.
patch
(
'avocado.utils.process.safe_kill'
)
...
...
@@ -466,12 +466,13 @@ class MiscProcessTests(unittest.TestCase):
get_children_pids
,
safe_kill
):
safe_kill
.
return_value
=
True
get_children_pids
.
return_value
=
[]
p_time
.
side_effect
=
[
500
,
502
,
504
,
506
,
508
,
510
,
512
,
514
,
516
,
518
]
p_time
.
side_effect
=
[
500
,
502
,
502
,
502
,
502
,
502
,
502
,
504
,
504
,
504
,
520
,
520
,
520
]
sleep
.
return_value
=
None
pid_exists
.
return_value
=
True
self
.
assertRaises
(
RuntimeError
,
process
.
kill_process_tree
,
1
,
self
.
assertRaises
(
RuntimeError
,
process
.
kill_process_tree
,
1
7
,
timeout
=
3
)
self
.
assert
Equal
(
p_time
.
call_count
,
5
)
self
.
assert
Less
(
p_time
.
call_count
,
10
)
@
mock
.
patch
(
'avocado.utils.process.safe_kill'
)
@
mock
.
patch
(
'avocado.utils.process.get_children_pids'
)
...
...
@@ -486,7 +487,7 @@ class MiscProcessTests(unittest.TestCase):
p_time
.
side_effect
=
[
500
,
502
,
502
,
502
,
502
,
502
,
502
,
502
,
502
,
503
]
sleep
.
return_value
=
None
pid_exists
.
side_effect
=
[
True
,
False
]
process
.
kill_process_tree
(
1
,
timeout
=
3
)
self
.
assertEqual
([
76
],
process
.
kill_process_tree
(
76
,
timeout
=
3
)
)
self
.
assertLess
(
p_time
.
call_count
,
10
)
@
mock
.
patch
(
'avocado.utils.process.safe_kill'
)
...
...
@@ -501,11 +502,23 @@ class MiscProcessTests(unittest.TestCase):
sleep
.
return_value
=
None
pid_exists
.
side_effect
=
[
True
,
True
,
True
,
True
,
True
,
False
]
process
.
kill_process_tree
(
1
,
timeout
=-
7.354
)
self
.
assertEqual
([
31
],
process
.
kill_process_tree
(
31
,
timeout
=-
7.354
)
)
self
.
assertEqual
(
pid_exists
.
call_count
,
6
)
self
.
assertEqual
(
sleep
.
call_count
,
5
)
@
mock
.
patch
(
'avocado.utils.process.time.sleep'
)
@
mock
.
patch
(
'avocado.utils.process.safe_kill'
)
@
mock
.
patch
(
'avocado.utils.process.get_children_pids'
)
def
test_kill_process_tree_children
(
self
,
get_children_pids
,
safe_kill
,
sleep
):
safe_kill
.
return_value
=
True
get_children_pids
.
side_effect
=
[[
53
,
12
],
[
78
,
58
,
41
],
[],
[
13
],
[],
[],
[]]
self
.
assertEqual
([
31
,
53
,
78
,
58
,
13
,
41
,
12
],
process
.
kill_process_tree
(
31
))
self
.
assertEqual
(
sleep
.
call_count
,
0
)
class
CmdResultTests
(
unittest
.
TestCase
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录