1

我有一些 python 代码目前通过许多线程并行执行计算来执行昂贵的计算。在给定的时间段内,许多线程被动态创建和启动,它们共享相同的代码,这些代码在线程的 run 方法中明确说明。我的问题是如何在其运行方法结束时停止/杀死一个线程?(运行只调用一次)我需要这样做以便为下一批计算创建更多线程。

#Example

class someThread(threading.Thread):
    def __init__(self):
        #some init code
    def run(self):

        #Explicitly Stated Code without constant loops

        #Something performed to stop/kill this thread
4

1 回答 1

2

您需要向主线程发出工作线程已完成的信号,然后调用join该线程。

于 2012-06-29T05:35:25.137 回答