python和ipython threading.activeCount()
我有一个模块,它导入线程并用于确定何时完成所有线程。我最初使用标准python解释器编写模块。在脚本中使用我的模块是可以的,但是在ipython中导入我的模块并调用依赖于threading.activeCount()的函数时。我的函数永远不会返回 代码: 我注意到,当第一次使用标准解释器导入线程并调用threading.activeCount()时,只计算1个线程:python和ipython threading.activeCount(),python,multithreading,ipython,Python,Multithreading,Ipython,我有一个模块,它导入线程并用于确定何时完成所有线程。我最初使用标准python解释器编写模块。在脚本中使用我的模块是可以的,但是在ipython中导入我的模块并调用依赖于threading.activeCount()的函数时。我的函数永远不会返回 代码: 我注意到,当第一次使用标准解释器导入线程并调用threading.activeCount()时,只计算1个线程: >>> import threading >>> threading.activeCount(
>>> import threading
>>> threading.activeCount()
1
>>> threading.enumerate()
[<_MainThread(MainThread, started 140344324941568)>]
导入线程
>>>threading.activeCount()
1.
>>>threading.enumerate()
[]
但是,使用ipython时,初始计数为2:
In [1]: import threading
In [2]: threading.activeCount()
Out[2]: 2
In [3]: threading.enumerate()
Out[3]:
[<_MainThread(MainThread, started 140674997614336)>,
<HistorySavingThread(Thread-1, started 140674935068416)>]
[1]中的:导入线程
在[2]中:threading.activeCount()
Out[2]:2
在[3]中:threading.enumerate()
出[3]:
[,
]
此模块由使用各种解释器的不同人员使用,因此我想知道是否有更好的方法来处理此问题(最好仍然使用线程)?您的线程,而不是依赖于activeCount
:
threads = []
for dev in run_list:
proc = threading.Thread(target=go, args=[dev])
proc.start()
threads.append(proc)
for proc in threads:
proc.join()
然后如何检查def main中的所有线程是否都已完成?现在我有一个While循环在运行,直到ActiveCount==0。如何使用join检查它?@Steelzeh join等待线程完成。在加入之后,你知道线程已经完成,而不需要通过其他方式进行检查。那么,如果我想在所有线程都完成后进行检查,这样我就可以显示一条消息或其他内容:)@Steelzeh是的,只需在加入所有线程后显示消息。
threads = []
for dev in run_list:
proc = threading.Thread(target=go, args=[dev])
proc.start()
threads.append(proc)
for proc in threads:
proc.join()