Python multi-threading (3) - Queue module
Queue module supports FIFO (FIFO) queue to support access to multiple threads, comprising a main types (Queue) and two exception classes (exception classes).
Python is renamed 2 Queue module in the queue in Python 3.
Create a Queue object
Queue object can be achieved by instantiating Queue Type:
1
|
q
=
Queue.Queue(maxsize
=
0
)
|
Create a new queue, meaning maxsize parameters are:
- If maxsize> 0: When q elements in the maxsize reached a queue on the full, until then there is a thread to which you want to insert, if the block option is specified, it will be blocked until a thread out of an element from the inside.
- If maxsize <= 0: Python think this is not a limitation of the capacity of the queue.
Queue module defines exception class
1
|
Queue.Empty
|
Q If the queue is empty, then turn calls the q.get (False), it will be thrown.
1
|
Queue.Full
|
Q If the queue is full, but call q.put (x, False), it will be thrown.
Queue object method
q.empty()
Determining whether the queue is empty.
q.full()
Whether the queue is full.
q.get(block=True, timeout=None) q.get_nowait()
Parameter block when False, the parameter timeout does not make sense, because the thread is not blocked:
- If the queue is not empty, and return the removed element;
- If the queue is empty, throw Queue.Empty
block is True, in conjunction with the timeout timeout to determine when the queue is empty, that has been blocking the process, or the process blocked for some time.
get_nowait () is equal to get (False), or get (timeout = 0) regardless of the queue empty No i.e., not blocked waiting.
E.g:
1
2
3
4
|
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)
Queue to insert item, if the queue is full, throw Queue.Full or thread blocked waiting for.
q.qsize()
Returns the current number of elements in the queue.
q.join()
q.task_done()
Queue module supports FIFO (FIFO) queue to support access to multiple threads, comprising a main types (Queue) and two exception classes (exception classes).
Python is renamed 2 Queue module in the queue in Python 3.
Create a Queue object
Queue object can be achieved by instantiating Queue Type:
1
|
q
=
Queue.Queue(maxsize
=
0
)
|
Create a new queue, meaning maxsize parameters are:
- If maxsize> 0: When q elements in the maxsize reached a queue on the full, until then there is a thread to which you want to insert, if the block option is specified, it will be blocked until a thread out of an element from the inside.
- If maxsize <= 0: Python think this is not a limitation of the capacity of the queue.
Queue module defines exception class
1
|
Queue.Empty
|
Q If the queue is empty, then turn calls the q.get (False), it will be thrown.
1
|
Queue.Full
|
Q If the queue is full, but call q.put (x, False), it will be thrown.
Queue object method
q.empty()
Determining whether the queue is empty.
q.full()
Whether the queue is full.
q.get(block=True, timeout=None) q.get_nowait()
Parameter block when False, the parameter timeout does not make sense, because the thread is not blocked:
- If the queue is not empty, and return the removed element;
- If the queue is empty, throw Queue.Empty
block is True, in conjunction with the timeout timeout to determine when the queue is empty, that has been blocking the process, or the process blocked for some time.
get_nowait () is equal to get (False), or get (timeout = 0) regardless of the queue empty No i.e., not blocked waiting.
E.g:
1
2
3
4
|
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)
Queue to insert item, if the queue is full, throw Queue.Full or thread blocked waiting for.
q.qsize()
Returns the current number of elements in the queue.
q.join()
q.task_done()