- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个导入线程并使用 threading.activeCount() 的模块确定所有线程何时完成。我最初使用标准 python 解释器编写我的模块。在脚本中使用我的模块很好,但是在 ipython 中导入我的模块并调用依赖于 threading.activeCount() 的函数时。我的函数永远不会返回。
代码:
for dev in run_list:
proc = threading.Thread(target=go, args=[dev])
proc.start()
while threading.activeCount() > 1:
time.sleep(1)
我注意到,当第一次使用标准解释器导入线程并调用 threading.activeCount() 时,只计算了 1 个线程:
>>> import threading
>>> threading.activeCount()
1
>>> threading.enumerate()
[<_MainThread(MainThread, started 140344324941568)>]
但是当使用 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)>]
这个模块被各种使用各种解释器的人使用,所以我想知道是否有更好的方法来处理这个问题(最好仍然使用线程)?
最佳答案
join
你的线程而不是依赖 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()
关于python 和 ipython threading.activeCount(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20575746/
Thread.activeCount() 和 ThreadGroup.activeCount() 有什么区别? oracle java 文档说这两种方法都会返回组(包括子组)中的估计线程数。当我得到列
ThreadGroup#activeCount() 的文档说:返回此线程组及其子组中 Activity 线程数量的估计值。 该计数是否包括处于 sleep 、 wait 和 join 模式的线程,还是
我有一个导入线程并使用 threading.activeCount() 的模块确定所有线程何时完成。我最初使用标准 python 解释器编写我的模块。在脚本中使用我的模块很好,但是在 ipython
我正在学习多线程的概念,我试图找到数组中 Activity 线程的数量,但是 ThreadGroup.activeCount() 的方法> 仅返回零值。 这是代码: 线程对象类:- class th1
这是我的申请: public class NamedThread extends Thread { /* This will store name of the thread */ S
我有以下代码片段: int k = 4; ExecutorService e = Executors.newFixedThreadPool(k); for (int i
我在 中有一个具有以下配置的 tomcat 实例 线程的catalina.properties server.service-Catalina.executor-tomcatThreadPool.ma
我是一名优秀的程序员,十分优秀!