gpt4 book ai didi

python - 多线程时如何关闭线程?

转载 作者:太空狗 更新时间:2023-10-30 02:08:21 24 4
gpt4 key购买 nike

<分区>

为了简化我遇到的情况:我试图在线程仍在 Python 2.7 中运行时终止它,但我不确定该怎么做。

使用这个简单的代码:

import time
import threading

def thread1():
print "Starting thread 1"
while True:
time.sleep(0.5)
print "Working"

thread1 = threading.Thread(target=thread1, args=())
thread1.start()

time.sleep(2)
print "Killing thread 1"
thread2.stop()
print "Checking if it worked:"
print "Thread is: " + str(thread1.isAlive())

线程 1 继续“工作”,我试图在主线程中终止它。关于如何做的任何想法?我试过:

threat1.terminate
threat1.stop
threat1.quit
threat1.end

这一切似乎都表明,没有办法用简单的一行代码来真正阻止它。你有什么建议?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com