I recently decided to work with Python 3 and I have questions regarding queues. As far as I understand the queue in the queue library is adapted for multithreaded programs. I assume that that queue is not as efficient as the simple queue (if I am wrong please correct). Therefore, I found that there is a deque from collections library which resembles deque in C++. Am I right that that's what competitive Python programmers use (for example, BFS problems)?
I program mostly in pypy and this is what i used for bfs.
Deque with popleft can easily replace queue and is fast for unbounded queue as it require no locking.
Another Alternative is to use Queue module itself
Useful Link
deque is the right queue for algorithms. Don't use Queue it is for communication between threads.
You can read the source code for Python 3
queue
module here.As you can see from source,
Queue
class adds locks forput
andget
methods. If you just want a simple double ended queue, you should usedeque
fromcollections
module. EvenQueue
usesdeque
internally.You are right in assuming that
Queue
is not as efficient asdeque
anddeque
is what Python programmers use.