041 queue (important, multithreading), 041 queue
Content: queue type, method, and usage
###############
Queue defines three types of information queuing Modes
Queue ([maxsize]): FIFO Queue Mode
LifoQueue ([maxsize]): LIFO queuing mode, stack Mode
###### Method
Q. qsize (): returns the queue size.
Q. full (): If the queue is full, True is returned; otherwise, False is returned.
Q. empty (): True is returned if the queue is empty; otherwise, False is returned.
Queue features: first-in-first-out, with a lock inside to ensure the security of multi-threaded data
Import queueq = queue. queue (3) # set the number of data items to be stored in q. put ('jsoning') q. put ('xming') q. put ('qqqq') q. put ('Q', 0) # The default value of the parameter at 0 is, indicating that the thread is blocked when the number of settings is exceeded. 0 indicates that print (q. get () print (q. get () print (q. get () print (q. get (0) #0 indicates that no error is reported. The default value is blocking.
View Code
Purpose: you do not need to use synchronization. There is a lock in the queue.
Take the following example:
1 from random import randint 2 import threading 3 from time import sleep 4 import queue 5 6 class Production (threading. thread): 7 def run (self): 8 while True: 9 r = randint (0,100) 10 q. put (r) 11 print ("produced % s steamed stuffed bun" % r) 12 sleep (1) 13 14 class Proces (threading. thread): 15 def run (self): 16 while True: 17 ret = q. get () 18 print ('eat % s Steamed Stuffed Bun '% ret) 19 20 if _ name _ =' _ main _ ': 21 q = queue. queue () 22 threads = [Production (), Proces ()] 23 for t in threads: 24 t. start ()
View Code