- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
关于文档中的示例: https://docs.python.org/2/library/queue.html#Queue.Queue.get
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
工作人员实际上如何知道所有工作都已完成,队列为空并且我们可以退出?没看懂。。。
最佳答案
您的 worker 在 while True:
循环中挂起,这意味着函数/线程将永远不会返回。
“魔法”在于您未显示的代码:
t = Thread(target=worker)
t.daemon = True
t.start()
daemon参数控制上层线程何时可以退出
The entire Python program exits when no alive non-daemon threads are left.
这意味着,程序将退出,因为主线程存在。工作线程会继续存在,但会在主线程结束时被销毁(因为“没有非守护线程留下”)。
主线程退出条件为
q.join()
join
的文档将在停止阻止执行时显示。
[...] When the count of unfinished tasks drops to zero, join() unblocks.
关于Python - Queue 究竟是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51134971/
Feel free to skip straight to TL/DR if you're not interested in details of the question 简短的序言: 我最近决定
我一直在阅读 A Tour of Go学习Go-Lang到目前为止一切顺利。 我目前在 Struct Fields类(class),这是右侧的示例代码: package main import "fm
Last time I got confused顺便说一下PowerShell急切地展开集合,基思总结了它的启发式如下: Putting the results (an array) within a
我是一名优秀的程序员,十分优秀!