The queue module supports FIFO (FIFO) queuing, which supports multi-threaded access, including a primary type (Queue) and two exception classes (exception classes).
The queue module in Python 2 was renamed queue in Python 3.
Creation of a Queue object
You can obtain a queue object by instantiating the queue type:
Q = Queue.queue (maxsize=0)
To create a new queue, the meaning of the parameter maxsize is:
- If maxsize > 0: When the element in Q reaches MaxSize, the queue is full, and when another thread wants to insert into it, if the block option is specified, it will block until a thread pulls an element out of it.
- If MaxSize <= 0:python would consider this to be a queue without capacity constraints.
Exception class defined by the queue module
Queue.empty
If the queue q is empty and Q.get (False) is called at this point, the exception is thrown.
Queue.full
If the queue q is full and the Q.put (x, False) is called, the exception is thrown.
Method of the Queue object
Q.empty ()
Determines whether the queue is empty.
Q.full ()
Determines whether the queue is full.
Q.get (block=true, timeout=None) q.get_nowait ()
Parameter timeout is meaningless when the parameter block is False because the thread does not block:
- If the queue is not empty, take away and return the element;
- If the queue is empty, throw queue.empty
When block is True, the time -out timeout is determined when the queue is empty, the process is blocked, or the process is blocked for some time.
Get_nowait () equals get (False), or get (timeout=0) does not block waiting, regardless of whether the queue is empty or not.
For example:
Try: x = q.get_nowait () except Queue.empty: print "No more items to process"
Q.put (item, block=true, timeout=None) q.put_nowait (item)
Inserts an item into the queue if the queue is full, throws a queue.full, or waits for a thread to block.
Q.qsize ()
Returns the number of elements in the current queue.
Q.join ()
Q.task_done ()
Python multithreaded (3)--queue module