我需要一个队列,多个线程可以往里存放数据,也可以从中读取。
Python 至少拥有两种队列类,Queue.Queue
和 collections.deque
。前者似乎在内部使用后者。两者在文档中都声称是线程安全的。
然而,队列文档也指出:
collections.deque is an alternative implementation of unbounded queues with fast atomic append() and popleft() operations that do not require locking.
我想我不太明白:这是否意味着deque并不完全线程安全?
如果是这样,我可能没有完全理解这两个类之间的区别。我可以看到Queue添加了阻塞功能。另一方面,它失去了一些deque的特性,比如支持in运算符。 如果是这样,我可能没有完全理解这两个类之间的区别。我可以看到Queue添加了阻塞功能。另一方面,它失去了一些deque的特性,比如支持in运算符。
访问内部deque对象直接,是
Queue().deque中的x。
线程安全? (Xiàn chéng ān quán?)
此外,为什么队列在操作时要使用互斥锁,因为双端队列已经是线程安全的了?