python队列,SimpleQueue,任务完成

2024-03-29 15:36:25 发布

您现在位置:Python中文网/ 问答频道 /正文

  1. 使用queue.Queue()时是否必须调用task_done()

  2. 如果我不需要任务计数,我是否应该使用queue.SimpleQueue(为了更好的性能)? 我不确定SimpleQueue是否是线程安全的?因为我看到put()实现没有获得锁

  3. 当我在Queue()实现中播种时,如果我不调用task_done(),则会有一个递增的计数器,并且由于我的程序正在长时间运行,不调用task_done()会导致一些内存泄漏吗?因为数数太多了


Tags: 内存程序taskqueueput计数器性能线程
2条回答
  1. 除非使用Queue.join()函数,否则不必调用task_done()

Queue.join() blocks until all items in the queue have been gotten and processed.

The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.

  1. 是的,如果不需要跟踪功能(task_donejoin),可以使用queue.SimpleQueue而不是queue.Queue作为更轻的版本SimpleQueue是线程安全的,而且更安全,如answered here

It handles reentrancy - it is safe to call queue.SimpleQueue.put in precarious situations where it might be interrupting other work in the same thread. For example, you can safely call it from __del__ methods, weakref callbacks, or signal module signal handlers.

注:至少这适用于其С实施

  1. 正如我所知,queue.Queue使用int作为put的计数器。我的拙见是,这个计数器可能会占用太多的内存,这更多的是理论上的,而不是实践上的

相关问题 更多 >