search

Home  >  Q&A  >  body text

java - python中queue的task_done和join起什么作用?

另外请问一下使用

queue.join()
queue.task_done()

while not workQueue.empty():
    pass

这两种方式来判断队列中是否还有任务的机制有区别吗?哪种更好呢?

PHPzPHPz2804 days ago896

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:59:50

    It depends on your design and usage scenarios.
    queue.join will block until all messages in the queue are retrieved and task_done is called before returning. Usually used to wait for all tasks to be processed and then exit the process.
    Empty will return immediately. When checking with your while loop, if the queue is empty, the thread will continue to loop endlessly. Waiting in a loop consumes a lot of CPU.

    reply
    0
  • Cancelreply