queue.Queue and collections.deque serve different purposes. queue.Queue is intended for allowing different threads to communicate using queued messages/data, whereas collections.deque is simply intended as a data structure. That's why queue.Queue has methods like put_nowait(), get_nowait(), and join(), whereas collections.deque doesn't. queue.Queue isn't intended to be used as a collection, which is why it lacks the likes of the in operator.
It boils down to this: if you have multiple threads and you want them to be able to communicate without the need for locks, you're looking for queue.Queue; if you just want a queue or a double-ended queue as a datastructure, use collections.deque.
Finally, accessing and manipulating the internal deque of a queue.Queue is playing with fire - you really don't want to be doing that.
queue.Queue and collections.deque serve different purposes. queue.Queue is intended for allowing different threads to communicate using queued messages/data, whereas collections.deque is simply intended as a data structure. That's why queue.Queue has methods like put_nowait(), get_nowait(), and join(), whereas collections.deque doesn't. queue.Queue isn't intended to be used as a collection, which is why it lacks the likes of the in operator.
It boils down to this: if you have multiple threads and you want them to be able to communicate without the need for locks, you're looking for queue.Queue; if you just want a queue or a double-ended queue as a datastructure, use collections.deque.
Finally, accessing and manipulating the internal deque of a queue.Queue is playing with fire - you really don't want to be doing that.
If all you're looking for is a thread-safe way to transfer objects between threads, then both would work (both for FIFO and LIFO). For FIFO:
Queue.put()andQueue.get()are thread-safe- Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
Note:
- Other operations on
dequemight not be thread safe, I'm not sure. dequedoes not block onpop()orpopleft()so you can't base your consumer thread flow on blocking till a new item arrives.
However, it seems that deque has a significant efficiency advantage. Here are some benchmark results in seconds using CPython 2.7.3 for inserting and removing 100k items
deque 0.0747888759791
Queue 1.60079066852
Here's the benchmark code:
import time
import Queue
import collections
q = collections.deque()
t0 = time.clock()
for i in xrange(100000):
q.append(1)
for i in xrange(100000):
q.popleft()
print 'deque', time.clock() - t0
q = Queue.Queue(200000)
t0 = time.clock()
for i in xrange(100000):
q.put(1)
for i in xrange(100000):
q.get()
print 'Queue', time.clock() - t0
Is there a good reason not to use deque when a queue will suffice?
Normal Queue vs Lists?
Is there a good reason not to use deque when a queue will ...
Use deque instead of list always ?
The article mentions the advantages of deque, but neglects the mention its disadvantages. While deque has fast pop/append from both ends, it has slow item access.
Use deque only if you need insert/remove to be fast from both ends, and don't care about read speeds. List has constant time append/pop from one end and also constant time access anywhere in the list. You should almost always prefer list over deque, which is why list is builtin.
More on reddit.comVideos
I recently learned about the deque class from collections in Python. I've been coding pretty haphazardly until now, not following any real design rules or anything, since I'm self taught and yprojects are for myself. Now I'm trying to tighten things up and bit and deciding if I should use a deque or queue to implement a command design pattern. Right now I see no particular need to append/pop from both sides but I'm tempted to use a deque just for the flexibility. Is there any reason I should stick with a 'single-end' plain queue?
Reading the code on a Queue
queue.Queue
This just seems like a fancy list initialized as a dequeue which only .get()s the first item of a list.
As I understand, both list.pop(0) and dequeue.popleft() operate in O(1).
The only difference that I notice is that when you pop(0) a list, it needs to move all the memory "to the side 1 space" so to speak. Is this the only real difference between dequeues and lists?
Should I just use a list as a FIFO, or is a list better for LIFO\Stack?