简体   繁体   English

如何在特定条件下杀死 python 线程

[英]how to kill a python thread under certain conditions

I am trying to start a thread using python threading module.我正在尝试使用 python 线程模块启动一个线程。 I want to kill this newly created thread under certain conditions.我想在某些条件下杀死这个新创建的线程。 I have written below code snippet but this seems to hang and process never returns我写了下面的代码片段,但这似乎挂起并且进程永远不会返回

from threading import Thread
import time
import os, signal
def sum():
    print("ada")
    time.sleep(5)
    print("ada1")

current = Thread(target=sum)
current.start()
current.join()
cur_pid = os.getpid()
print(cur_pid)
os.kill(cur_pid, signal.SIGSTOP)
print(current)

You'll want to send SIGKILL or SIGTERM , not SIGSTOP .您需要发送SIGKILLSIGTERM ,而不是SIGSTOP

SIGSTOP pauses the process (which sounds like what you're seeing). SIGSTOP暂停该过程(听起来像您所看到的)。

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

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