简体   繁体   English

在一段时间后终止子流程

[英]terminating subprocess after amount of time

I have two binaries which are not terminating without pressing CTRLC-C on terminal. 我有两个没有在终端上按CTRLC-C不会终止的二进制文件。 I call these binaries with subprocess call. 我用子流程调用来调用这些二进制文件。 But I want to terminate them after some amount of time. 但是我想在一段时间后终止它们。 Here is what I try: 这是我尝试的方法:

    cmd_1 = [**SOME COMMANDS**]
    cmd_2 = [**SOME COMMANDS**]
    commands = [cmd_1, cmd_2]
    procs = [Popen(i) for i in commands]
    for p in procs:
        time.sleep(60)
        p.kill();

But after 60 seconds, processes are still running. 但是60秒后,进程仍在运行。 How can I terminate them after some amount of time? 一段时间后如何终止它们?

The first process will be killed after 60 seconds, the second will after 120. The time.sleep(60) should be outside the loop 第一个进程将在60秒后终止,第二个进程将在120秒后终止。time.sleep(60)应该在循环之外

cmd_1 = [**SOME COMMANDS**]
cmd_2 = [**SOME COMMANDS**]
commands = [cmd_1, cmd_2]
procs = [Popen(i) for i in commands]
time.sleep(60)
for p in procs:
    p.kill()

However, it is worth noting that that **SOME COMMANDS** could be causing further issue 但是,值得注意的是,“ **SOME COMMANDS**可能会引起进一步的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM