Python:调用 process.terminate() 不会杀死它
当我运行以下代码时:
p = subprocess.Popen("...", shell=True)
if p.poll() == None:
p.kill()
该进程根本没有被终止。我在 Windows 上。
我以为是多线程的原因,我在单线程中运行它,仍然是同样的事情。
您知道为什么会发生这种情况吗?
谢谢
更新,
我找到了导致问题的代码:
while cur_time < self.time_limit:
if p.poll() != None:
too_much_time = False
break
time.sleep(0.1)
cur_time += 0.1
我运行此代码以确保该过程不会超过时间限制。显然这就是我无法终止该进程的原因。
When I run the following code:
p = subprocess.Popen("...", shell=True)
if p.poll() == None:
p.kill()
The process is simply not killed. I'm on Windows.
I thought it was because of multithreading, I ran it in a single thread, still same thing.
Do you have any ideas why this could be happening?
Thanks
Update
I found the code that causes the problem:
while cur_time < self.time_limit:
if p.poll() != None:
too_much_time = False
break
time.sleep(0.1)
cur_time += 0.1
I run this to make sure that the process doesn't take more than the time limit. Apparently that's why I can't kill the process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您使用
shell=True
,所以您最终杀死的只是 shell(cmd.exe 进程)本身。进程组是 Windows 的一个相对较新的功能,除了任务计划程序之外,我不知道还有什么软件实际使用它们。
Because you use
shell=True
, all you end up killing is the shell (cmd.exe process) itself.Process groups are a relatively new feature of Windows and I don't know of any software apart from Task Scheduler that actually uses them yet.
您正在运行的命令可能正在设置 sigmask 以忽略终止信号。您可以尝试使用其他命令吗?例如,制作一个无限休眠的脚本并尝试杀死那个
离题的内容,我认为 shell arg 在 Windows 上没有任何意义
It may be that the command you are running is setting the sigmask to ignore the kill signal. Can you try with another command? e.g make a script that sleeps in an infinite while and try to kill that
offtopic, I think shell arg doesn't mean anything on windows