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.deque, with the former seemingly using the latter internally. Both claim to be thread-safe in the documentation.
However, the Queue docs also state:
collections.deque is an alternative implementation of unbounded queues with fast atomic append() and popleft() operations that do not require locking.
Which I guess I don't quite unterstand: Does this mean deque isn't fully thread-safe after all?
If it is, I may not fully understand the difference between the two classes. I can see that Queue adds blocking functionality. On the other hand, it loses some deque features like support for the in-operator.
Accessing the internal deque object directly, is
x in Queue().deque
thread-safe?
Also, why does Queue employ a mutex for it's operations when deque is thread-safe already?

本文探讨了Python中Queue.Queue与collections.deque的区别与应用场景。Queue.Queue适用于多线程间的数据传递,提供阻塞功能;而deque则作为高效的数据结构使用,支持快速的两端操作。

894







RuntimeError: deque mutated during iterationis what you could be getting is using a shareddequebetween several thread and no locking... – toine Nov 7 '15 at 16:28