gpt4 book ai didi

python - 如何从内部关闭线程?

转载 作者:IT老高 更新时间:2023-10-28 21:34:50 27 4
gpt4 key购买 nike

对于每个连接到我的服务器的客户端,我都会生成一个新线程,如下所示:

# Create a new client
c = Client(self.server.accept(), globQueue[globQueueIndex], globQueueIndex, serverQueue )

# Start it
c.start()

# And thread it
self.threads.append(c)

现在,我知道我可以使用以下代码关闭所有线程:

    # Loop through all the threads and close (join) them
for c in self.threads:
c.join()

但是我怎样才能从那个线程inin关闭线程呢?

最佳答案

当你启动一个线程时,它会开始执行你给它的函数(如果你正在扩展threading.Thread,函数将是run())。要结束线程,只需从该函数返回即可。

根据this ,也可以调用thread.exit(),会抛出异常,静默结束线程。

关于python - 如何从内部关闭线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4541190/

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