Thread Operation 121 python program - queue queue

A thread queue

queue Queue: Queue to use the same process as

If necessary to securely exchange information between a plurality of threads, the thread queues is particularly useful in programming.

important:

q.put():To which put the value of the queue when the parameter block = Ture when the timeout parameter will have a role when the queue is full time, fill it up in time value, block program will wait for the timeout True, over time program will complain, when the block if Flase, the program does not wait for an error directly

q.get():Value from the queue which, when the parameter block = Ture when timeout parameter will have a role when the queue is empty when the value at the time of from the inside, block is True program will wait for timeout, over time program will complain, when the block if Flase, the program does not wait for an error directly

q.task_done(): Using this method the user signal that the q.get () returns the item has been processed. If the number of calls to this method is greater than the number of items removed from the queue, it will raise a ValueError exception.

q.join(): Producers will use this method blocks until all the items in the queue have been processed. Blocking will continue to queue for each item are calling q.task_done up method ().

Second, the way the value of the queue thread

2.1 FIFO

class queue.Queue(maxsize=0)

q = queue.Queue():No arguments on behalf of queues can put unlimited data

q = queue.Queue() #不加参数代表队列可以无限的放数据
q.put('Cecilia陈')
q.put('xichen')

print(q.get())
print(q.get())

'''
结果:
Cecilia陈
xichen
'''

2.2 LIFO

class queue.LifoQueue(maxsize=0)

q = Lifo.Queue():No arguments on behalf of queues can put unlimited data

q = queue.LifoQueue() #不加参数代表队列可以无线的放数据
q.put('Cecilia陈')
q.put('xichen')

print(q.get())
print(q.get())
'''
结果:
xichen
Cecilia陈
'''

Third, you can set the priority data stored in the queue

class queue.priorityQueue(maxsize=0)

q = queue.priorityQueue():No arguments on behalf of queues can put unlimited data

3.1 priority queue

# 3.优先级队列
q = queue.PriorityQueue() #不加参数代表队列可以无线的放数据
#put进入一个元组,元组的第一个元素是优先级(通常是数字,也可以是非数字之间的比较),数字越小优先级越高
q.put((2,'Cecilia陈'))
q.put((1,'xichen'))
q.put((5,'xuchen'))

print(q.get())
print(q.get())
print(q.get())
'''
# 结果(数字越小优先级越高,优先级高的优先出队):
结果:
(1, 'xichen')
(2, 'Cecilia陈')
(5, 'xuchen')
'''

3.2 Method Description

maxsizeIt is an integer, provided that the maximum number can be placed in the queue entry. Once this size is reached, the insertion will block until use queue entries. If maxsize is less than or equal to zero, then the queue size is infinite.

p.put():When the value of the discharge, the discharge is a tuple ()

exception queue.Empty: Exception Queue. Empty: throw an exception when the queue object-to-air non-blocking calls to get () or get_nowait () time, that queue is empty, then the value will error

exception queue.Full:An exception queue. Full: throw an exception when calling a non-blocking put () or put_nowait () to a full queue object. That's when the queue is full, then entered, when the value of the time will be thrown

Queue.qsize ():

empty(): If empty, returns True

Queue.full():If full, return True

put_nowait(item):Equivalent to put (item, False).

get_nowait():Equivalent to get (False).

Whether provides two ways to support the tracking queued task has been fully processed daemon consumer threads:

task_done():He expressed join the queue before the task has been completed. Use the queue consumer thread. For each get used to get task (), a subsequent call to task_done () tells the queue processing task has been completed. If you join (the current is blocked, then after handling all items, it will continue to run (which means that for each item has been placed in a queue (), have received task_done () call).

If the number of calls exceeds the number of items placed in the queue Raises ValueError.

Queue.join():Consumption block until the queue is completed.

Guess you like

Origin www.cnblogs.com/xichenHome/p/11569106.html