Python Queue Module Queue()














































Python Queue Module Queue()




  Previous article link:-
----------------------------------------------------------------------------------------------------------------------------------
queue.Queue()

  Description:-

  The queue.Queue() class follow the First in, First out principle. It has one end through which data
   is inserted using put() and the data is removed from the other end using get(). It can be consider
   with that of an open cylinder from the both side. In thread communication it can be used as follows:-
                 
                 Producer put the data/function through one end and the consumer retrieve the data from 
                 the other end

  Python queue installation:-

  1) You just have the queue module using 'import' in your workspace.

import queue
         The module is available by default in python you need not to install it separately.

  2) To work with FIFO queue, call the Queue class using queue module  imported as shown below:-

import queue
q1 = queue.Queue()
  SYNTAX:- queue.Queue(maxsize=0)
                  If you doesn't mention anything in the argument then that means the maxsize = 0(by
                  default). If you mention it then you can not place items more then the maxsize.
   
  Methods available inside Queue class.
  
  A) put(item) - Put item inside the queue.
  B) get() - This will return you an item from the queue.
  C) empty() - Return True is queue is empty and False if if present.
  D) qsize() - Returns the size of the queue.
  E) full() - Returns True if queue is full and False if empty.
 

  NOTE - In the previous article we discussed in short about these methods. They all are available         under the queue class.  

  Let's have a look at some of the examples to have a better understanding of Queue class and
  the methods available inside it.

  EXAMPLE-1 (Basic FIFO example)
   
#Program to demonstrate basic FIFO operation using Queue class

import queue #Queue module imported in the workspace


def main():
q = queue.Queue() #Queue is created

for i in range(0, 10, 2):
q.put(i) #Items placed into the queue

while not q.empty():
print("Removed item from queue:", end=' ')
print(q.get()) #Items retrieved from the queue


if __name__ == "__main__":
main() #Main function called

     OUTPUT:

"C:UsersShubhani_PandeyDesktopHarshit Pandeypythonpython.exe" "C:/Users/Shubhani_Pandey/Desktop/Harshit Pandey/python_files/test.py"
Removed item from queue: 0
Removed item from queue: 2
Removed item from queue: 4
Removed item from queue: 6
Removed item from queue: 8

Process finished with exit code 0
     This example uses a single thread and it illustrates that the elements are retrieved in the same 
     order in which they are inserted (i.e. inserted order- 0,2,4,6,8 and retrieved in the same order i.e.
     0,2,4,6,8)

  NOTE:- In the above example maxsize is not mentioned in the argument. That means the queue is     infinite and we can add as many items as we want.


  EXAMPLE-2 

import queue


def main():
q = queue.Queue(
maxsize=5) #maxsize of queue is initialized
print("Queue size:", q.qsize()) #size of queue before inserting elements

for i in range(0, 10, 2):
print("Full:", q.full()) #Return True if queue is full otherwise False
q.put(i)
print("Queue size:", q.qsize()) #size of queue before inserting elements
print("Full:", q.full()) #Returns True or False after putting elements queue

while not q.empty():
print("Removed item from queue:", end=' ')
print(q.get())
print("Empty:", q.empty()) #Return True if queue is full otherwise False
print("Queue size:", q.qsize()) #Return True if queue is empty otherwise False



if __name__ ==
"__main__":
main()

     OUTPUT:

"C:UsersShubhani_PandeyDesktopHarshit Pandeypythonpython.exe" "C:/Users/Shubhani_Pandey/Desktop/Harshit Pandey/python_files/test.py"
Queue size: 0
Full: False
Full: False
Full: False
Full: False
Full: False
Queue size: 5
Full: True
Removed item from queue: 0
Empty: False
Removed item from queue: 2
Empty: False
Removed item from queue: 4
Empty: False
Removed item from queue: 6
Empty: False
Removed item from queue: 8
Empty: True
Queue size: 0

Process finished with exit code 0

  NOTE:- Maxsize of queue is mentioned in the above example(i.e. 5). that means queue size is finite and     more than 5 elements would not be inserted

  Above example includes everything that we have discussed in this article. It returns the size of the 
  queue, returns if queue is full or not after every step and also if queue is empty or not.
  
  That's all for this article. Will discuss more classes and methods in them in the next article.
 
  Next article link:-

Comments