Queue中的join和task_done方法

Queue.task_done() 与Queue.join()配合使用,在完成一项工作之后,会向任务已经完成的队列发送一个信号,Queue.join() 实际上意味着等到队列为空,再执行其他操作。


如果线程里每从队列里取一次,但没有执行task_done(),则join无法判断队列到底有没有结束,在最后执行join()是等不到信号结果的,会一直挂起。即每task_done一次 就从队列里删掉一个元素,这样join的时候根据队列长度是否为零来判断队列是否结束,从而执行其他操作。

例子:

[Python]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import threading
import queue
from time import sleep
class Mythread(threading.Thread):
  def __init__( self ,que):
  threading.Thread.__init__( self )
  self .queue = que
  def run( self ):
  while True :
  sleep( 1 )
  if self .queue.empty():
  break
  item = self .queue.get()
  print (item, '!' )
self .queue.task_done()
  return
que = queue.Queue()
tasks = [Mythread(que) for x in range ( 1 )]
for x in range ( 10 ):
  
  que.put(x)
for x in tasks:
  t = Mythread(que)
  t.start()
  
que.join()



快速生产-快速消费
上面的演示代码是快速生产-慢速消费的场景,我们可以直接用task_done()与join()配合,来让empty()判断出队列是否已经结束。 当然,queue我们可以正确判断是否已经清空,但是线程里的get队列是不知道,如果没有东西告诉它,队列空了,因此get还会继续阻塞,那么我们就需要在get程序中加一个判断,如果empty()成立,break退出循环,否则get()还是会一直阻塞。

慢速生产-快速消费
但是如果生产者速度与消费者速度相当,或者生产速度小于消费速度,则靠task_done()来实现队列减一则不靠谱,队列会时常处于供不应求的状态,常为empty,所以用empty来判断则不靠谱。 那么这种情况会导致 join可以判断出队列结束了,但是线程里不能依靠empty()来判断线程是否可以结束。 我们可以在消费队列的每个线程最后塞入一个特定的“标记”,在消费的时候判断,如果get到了这么一个“标记”,则可以判定队列结束了,因为生产队列都结束了,也不会再新增了。 代码如下:

[Python]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import threading
import queue
from time import sleep
class Mythread(threading.Thread):
  def __init__( self ,que):
  threading.Thread.__init__( self )
  self .queue = que
  def run( self ):
  while True :
  item = self .queue.get()
  self .queue.task_done()
  if item = = None :
  break
  print (item, '!' )
return
que = queue.Queue()
tasks = [Mythread(que) for x in range ( 1 )]
 
for x in tasks:
  t = Mythread(que)
  t.start()
for x in range ( 10 ):
  sleep( 1 )
  que.put(x)
for x in tasks:
  que.put( None )

 

更多技术资讯可关注:gzitcast

猜你喜欢

转载自www.cnblogs.com/heimaguangzhou/p/11578575.html