Threading.start()并不总是开始线程的进程

时间:2019-05-29 21:56:36

标签: python multithreading python-multithreading

我有一个程序需要收集和显示数据,并且正在处理大量数据,所以我正在使用线程加速它。

问题是,当调试有时运行thread.start()时,并不会立即调用thread.run()

例如,有时thread.start()将立即调用thread.run(),而有时thread2.run()完成执行后将调用thread.run()。

有时似乎从未调用过thread.run(),并且在尝试加入线程时该程序超时。

def do_work(self, values, toSearch):
    threads = []
    for i in range(3):
        query = MyThreadClass(values, toSearch)
        query.start()
        threads.append(query)

    for t in threads:
        t.join()
        if t.is_alive():
            print "Thread is still alive!"
#           At this point at least one thread is still alive often, however
#           not ALL the time

我希望每次对query.start()的调用都会立即运行query.run(),除非不是这种情况。

当我到达.join()时,仍然有一个线程处于活动状态,我的程序将超时。

调试表明,调用run()的每个对象都会被停止,但是从未调用run()的每个对象都不会停止,并挂在join()上。

为什么会发生这种情况/我该如何解决join()上的超时问题?

0 个答案:

没有答案