Nat*_*man 7 python queue multithreading
我的应用程序中有两个线程.一个将值放在a中Queue,另一个将它们从中Queue处理并处理它们.
关闭应用程序时,我面临两难选择.处理中的项目的线程Queue被卡住:
item = request_queue.get() # this call blocks until an item is available
Run Code Online (Sandbox Code Playgroud)
唯一能够终止线程的是如果另一个项被添加到Queue- 并且由于主线程没有添加任何东西(因为它正在关闭),应用程序会锁定.
那么...... Queue.get()即使什么都没有,我怎么能指示以某种方式返回Queue呢?
事实证明答案很简单.选择一个对处理Queue(None理想的代码)的代码无效的值并将其推入Queue.然后让Queue处理线程在获取值时退出:
while True:
item = request_queue.get()
if item is None:
break
# process the Queue as per normal...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3937 次 |
| 最近记录: |