Thread's queue
In Python, queues are the most common form of exchanging data between threads. The queue module is a module that provides queued operations, although it's easy to use, but if you're not careful, there are some surprises.
create a ' queue ' object
Import queue
Q = queue. Queue (3) #Queue (Maxsizse = 3)
The Queue.queue class is a synchronous implementation of a queue. The queue length can be unlimited or limited. The queue length can be set through the optional parameter maxsize of the queue's constructor. If MaxSize is less than 1, it means that the queue length is infinite.
put a value in the queue
Q.put (10) calls the put () method of the queue object to insert an item at the end of the team. Put () has two parameters, the first item is required, the value of the inserted item, the second block is an optional parameter, and the default is 1. If the queue is currently empty and the Block is the 1,put () method, the calling thread pauses until a data cell is vacated. If the block is the 0,put method, the full exception is thrown.
Q.put (3)
Q.put (' Zhang ')
Q.put ([' Zhang ', ' Li '])
# q.put (6,block=false)
Print (Q.get ())
Q.put (6,block=true,timeout=2)
# Q.put (8)
The block defaults to True when the queue is full. So when you insert data into a full queue, the queue blocks and the thread snaps. When the Block=false is in place, an error occurs when you put the program again. In the timeout parameter setting, q.put (6,block=true,timeout=2) means that when the queue has not been removed for two seconds, the program will get an error. In an attempt to
Thread's queue