Both queue.queue and collections.deque commands give an idea about queues in general to the reader but, both have a very different application hence shouldn’t be confused as one. Although they are different and used for very different purposes they are in a way linked to each other in terms of complete functionality. Before we jump into what they actually do and how they are linked to each other, there is one concept that has to be revisited, the basics of processing in computer software.
We know, that any program becomes a process in active state and each process can be broken down to threads to reap the benefits of the advantage this possesses. We also know, that two threads may have to communicate with each other and this is where queue.queue comes into the picture. Collections.deque on the other hand is used as a data structure within a thread to perform certain functionality. The link between them is that queue.queue uses collections.deque internally. Both deal with thread-safe operations.
Queue.Queue: This class as stated above is used to facilitate communication between two threads originating from the same process. It works like a typical queue though, the only difference being the purpose it serves. It has all the functions of multirocessing.queue to do so, along with two more functions- task_done() and join().
- As the name suggests, task_done() is used to inform task completion
- Join() is used to ask all the tasks to wait until all processes are done processing.
all tasks sent task 0 started task 0 finished task 1 started task 1 finished task 2 started task 2 finished task 3 started task 3 finished task 4 started task 4 finished all tasks completed
Collections.Deque: A general data structure, which behaves like a regular FIFO Queue. This is employed within a thread to get some functionality done. Its basic implementation is shown below:
deque([‘first’, ‘second’, ‘third’])
deque([‘zeroth’, ‘first’, ‘second’, ‘third’, ‘fourth’])
deque([‘fifth’, ‘zeroth’, ‘first’, ‘second’, ‘third’, ‘fourth’])
deque([‘fifth’, ‘zeroth’, ‘first’, ‘second’, ‘third’])
deque([‘fifth’, ‘first’, ‘second’, ‘third’])
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python set operations (union, intersection, difference and symmetric difference)
- Difference between various Implementations of Python
- Difference between Method and Function in Python
- Python | Difference between iterable and iterator
- Difference between List and Array in Python
- Python | Difference between Pandas.copy() and copying through variables
- Python program to find sum of absolute difference between all pairs in a list
- Difference between List comprehension and Lambda in Python
- Difference between Python and C#
- Difference between C and Python
- Python | Calculate difference between adjacent elements in given list
- Python | Difference between two dates (in minutes) using datetime.timedelta() method
- Python | Difference Between List and Tuple
- Difference Between Go and Python Programming Language
- Python | Find Maximum difference between tuple pairs
- Difference between continue and pass statements in Python
- Difference between input() and raw_input() functions in Python
- Difference between dict.items() and dict.iteritems() in Python
- Python - Difference between sorted() and sort()
- Difference between List and Dictionary in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.