Open In App

Difference between queue.queue vs collections.deque in Python

Last Updated : 22 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Python3




# import modules
import threading, queue
 
# setting up a queue for threads
q = queue.Queue()
 
def execute_thread():
    while True:
        th=q.get()
        print(f'task {th} started')
        print(f'task {th} finished')
        q.task_done()
 
# set up for threads to work
threading.Thread(target = execute_thread,
                 daemon = True).start()
 
# sending task requests
for i in range(5):
    q.put(i)
print("all tasks sent")
 
 
# making threads wait until all tasks are done
q.join()
print("all tasks completed")


Output:

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

The time complexity of this code is O(n), where n is the number of tasks.
The auxiliary space complexity of this code is O(1), as it uses a constant amount of memory throughout its execution. 

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:

Python3




# import module
from collections import deque
 
# initialise
dq = deque(['first','second','third'])
print(dq)
deque(['first', 'second', 'third'])
 
# adding more values
dq.append('fourth')
dq.appendleft('zeroth')
print(dq)
deque(['zeroth', 'first', 'second', 'third', 'fourth'])
 
# adding value to a specified index
dq.insert(0,'fifth')
print(dq)
deque(['fifth', 'zeroth', 'first', 'second', 'third', 'fourth'])
 
# removing values
dq.pop()
'fourth'
print(dq)
deque(['fifth', 'zeroth', 'first', 'second', 'third'])
dq.remove('zeroth')
print(dq)
deque(['fifth', 'first', 'second', 'third'])


Output:

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’])



Similar Reads

Difference between Queue and Deque (Queue vs. Deque)
Queue: The queue is an abstract data type or linear data structure from which elements can be inserted at the rear(back) of the queue and elements can be deleted from the front(head) of the queue. The operations allowed in the queue are:insert an element at the reardelete element from the frontget the last elementget the first elementcheck the size
3 min read
Python - Queue.LIFOQueue vs Collections.Deque
Both LIFOQueue and Deque can be used using in-built modules Queue and Collections in Python, both of them are data structures and are widely used, but for different purposes. In this article, we will consider the difference between both Queue.LIFOQueue and Collections.Deque concerning usability, execution time, working, implementation, etc. in Pyth
3 min read
Creating Queue Using Collections.Deque In Python
In Python, Queue and Deque are the data structures used for managing collections of elements in a first-in, first-out (FIFO) manner. In this article, we will learn to implement queues using collections.deque in Python. Create Queue Using Collections.deque In PythonBelow, is the Implementation of Creating Queue Using Collections.Deque in Python: Ste
3 min read
Difference Between deque::cbegin and deque::assign in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::at in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
Difference Between deque::begin and deque::assign in C++
Deque or Double-ended queue are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the difference
3 min read
Difference Between deque::assign and deque::empty in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
2 min read
Difference between Queue and Deque in C++
Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other. Functions: empty(): Tests whether the queue is empty.size(): Returns the unsigned int, size of the que
4 min read
Difference between Multilevel Queue (MLQ) and Multi Level Feedback Queue (MLFQ) CPU scheduling algorithms
In multi programming environment, it often happens that more than one processes compete for CPU resources at the same time. If only one CPU is available choice has to be made between processes to run next. Part of the Operating System responsible for making choice of process is called Scheduler and the algorithm it used is called Scheduling Algorit
3 min read