[英]how to kill a python thread under certain conditions
我正在嘗試使用 python 線程模塊啟動一個線程。 我想在某些條件下殺死這個新創建的線程。 我寫了下面的代碼片段,但這似乎掛起並且進程永遠不會返回
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)
您需要發送SIGKILL
或SIGTERM
,而不是SIGSTOP
。
SIGSTOP
暫停該過程(聽起來像您所看到的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.