Queue.Queue vs. collections.deque

前端 未结 7 1337
眼角桃花
眼角桃花 2020-11-29 15:26

I need a queue which multiple threads can put stuff into, and multiple threads may read from.

Python has at least two queue classes, Queue.Queue and collections.dequ

相关标签:
7条回答
  • 2020-11-29 15:54

    (seems I don't have reputation to comment...) You need to be careful which methods of the deque you use from different threads.

    deque.get() appears to be threadsafe, but I have found that doing

    for item in a_deque:
       process(item)
    

    can fail if another thread is adding items at the same time. I got an RuntimeException that complained "deque mutated during iteration".

    Check collectionsmodule.c to see which operations are affected by this

    0 讨论(0)
提交回复
热议问题