Details of the Python Queue module
- 2020-04-02 14:27:16
- OfStack
In Python, queues are the most common form of exchanging data between threads. The Queue module is a module that provides Queue operations. Although it is easy to use, there are still some surprises if you are not careful.
Create a "queue" object
The import Queue
Q = Queue.Queue(maxsize = 10)
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 the constructor of the Queue. If the maxsize is less than 1, the queue length is infinite.
Queue a value
Q.p ut (10)
The call to the queue object's put() method inserts an item at the end of the queue. Put () has two parameters, the first item is required, is the value of the inserted item; The second block is an optional parameter and defaults to
1. If the queue is currently empty and the block is 1, the put() method suspends the calling thread until a data cell is vacated. If the block is 0, the put method throws a Full exception.
Fetches a value from the queue
Q.g et ()
Call the get() method of the queue object to remove from the queue header and return an item. The optional parameter is block and the default is True. If the queue is empty and the block is True, get() suspends the calling thread until an item is available. If the queue is Empty and the block is False, the queue throws an Empty exception.
The Python Queue module has three types of queues and constructors:
1. FIFO first-in-first-out Queue of Python Queue module The class Queue. The Queue (maxsize)
2. LIFO is similar to a heap in that it is first in and then out. The class Queue. LifoQueue (maxsize)
3. There is another way that the lower the priority queue is, the earlier it comes out. The class Queue. PriorityQueue (maxsize)
Common methods in this package (q = queue.queue ()):
Q.qsize () returns the size of the queue
Q.emty () returns True if the queue is empty and False otherwise
Q.ull () returns True if the queue is full and False otherwise
Q.ull corresponds to maxsize
Q.g et([block[, timeout]]) gets the queue, timeout wait time
Q.g et_nowait () quite q.g et (False)
Non-blocking q.ut (item) write queue, timeout wait time
Q.pro_nowait (item) is equivalent to q.prot (item, False)
After a work is done, the q.ask_done () function sends a signal to the queue that the task has completed
Q.join () essentially means waiting until the queue is empty before doing anything else
Example:
Implement a thread to continuously generate a random number to a Queue (consider using the Queue module)
Implementation of a thread from the queue above the odd number out
Implement another thread to pull out even Numbers 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) # Store the data in a queue 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()