Python Queue module details and examples

  • 2020-05-19 05:02:36
  • OfStack

Python Queue module

In Python, queues are the most common form of data exchange between threads. Queue module is a module that provides queue operation. Although it is easy to use, if you are not careful, there will still be some surprises.

Create a "queue" object


import Queue
q = Queue.Queue(maxsize = 10)

The Queue.Queue class is a synchronous implementation of a queue. The queue length can be infinite or finite. The queue length can be set by maxsize, an optional parameter to Queue's constructor. If maxsize is less than 1, the queue is infinite.

Put 1 value in the queue


q.put(10)

Call the put() method of the queue object to insert an item at the end of the queue. put() has two parameters. The first item is required and is the value of the insert item. The second block is optional and defaults to 1. If the queue is currently empty and block is 1, the put() method suspends the calling thread until a data cell is vacated. If block is 0, the put method throws an Full exception.

Take 1 value out of the queue


q.get()

The call queue object's get() method is removed from the queue header and returns an item. The optional parameter is block, and the default is True. If the queue is empty and block is True, get() suspends the calling thread until an item is available. If the queue is empty and block is False, the queue throws an Empty exception.

The Python Queue module has three types of queues and constructors:

1. Python Queue module FIFO queue fifo. class Queue. Queue (maxsize)
2. LIFO is similar to heap, i.e., first in, then out. class Queue. LifoQueue (maxsize)
3. Another is that the lower the level of the priority queue, the earlier it comes out. class Queue. PriorityQueue (maxsize)

Common methods in this package (q = Queue.Queue ()):


q.qsize()  Returns the size of the queue 
q.empty()  If the queue is empty, return True, And vice False
q.full()  If the queue is full, return True, And vice False
q.full  with  maxsize  The size of the corresponding 
q.get([block[, timeout]])  Get the queue, timeout Waiting time 
q.get_nowait()  Quite a q.get(False)
 non-blocking  q.put(item)  Write to the queue, timeout Waiting time 
q.put_nowait(item)  Quite a q.put(item, False)
q.task_done()  At the completion of 1 After the work, q.task_done()  The function sends to a queue where the task has completed 1 A signal 
q.join()  In effect, it means waiting until the queue is empty before doing anything else 

Example:

Implement 1 thread to continuously generate 1 random number to 1 queue (consider using the module Queue)

Implement 1 thread from the above queue out of the odd number

Implement another thread to pull an even number from the queue above


#!/usr/bin/env python
#coding:utf8
import random,threading,time
from Queue import Queue
#Producer thread
class Producer(threading.Thread):
  def __init__(self, t_name, queue):
    threading.Thread.__init__(self,name=t_name)
    self.data=queue
  def run(self):
    for i in range(10):  # Randomly generated 10 A digital   , can be modified to any size 
      randomnum=random.randint(1,99)
      print "%s: %s is producing %d to the queue!" % (time.ctime(), self.getName(), randomnum)
      self.data.put(randomnum) # Queue the data in sequence 
      time.sleep(1)
    print "%s: %s finished!" %(time.ctime(), self.getName())
 
#Consumer thread
class Consumer_even(threading.Thread):
  def __init__(self,t_name,queue):
    threading.Thread.__init__(self,name=t_name)
    self.data=queue
  def run(self):
    while 1:
      try:
        val_even = self.data.get(1,5) #get(self, block=True, timeout=None) ,1 That's blocking waiting ,5 Is the timeout 5 seconds 
        if val_even%2==0:
          print "%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(),self.getName(),val_even)
          time.sleep(2)
        else:
          self.data.put(val_even)
          time.sleep(2)
      except:   # Wait for input, over 5 seconds   So unusual 
        print "%s: %s finished!" %(time.ctime(),self.getName())
        break
class Consumer_odd(threading.Thread):
  def __init__(self,t_name,queue):
    threading.Thread.__init__(self, name=t_name)
    self.data=queue
  def run(self):
    while 1:
      try:
        val_odd = self.data.get(1,5)
        if val_odd%2!=0:
          print "%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(), self.getName(), val_odd)
          time.sleep(2)
        else:
          self.data.put(val_odd)
          time.sleep(2)
      except:
        print "%s: %s finished!" % (time.ctime(), self.getName())
        break
#Main thread
def main():
  queue = Queue()
  producer = Producer('Pro.', queue)
  consumer_even = Consumer_even('Con_even.', queue)
  consumer_odd = Consumer_odd('Con_odd.',queue)
  producer.start()
  consumer_even.start()
  consumer_odd.start()
  producer.join()
  consumer_even.join()
  consumer_odd.join()
  print 'All threads terminate!'
 
if __name__ == '__main__':
  main()


Related articles: