Basic FIFO queue: FIFO first.
CALSS Queue.queue (maxsize=0)
MaxSize is an integer that indicates the maximum number of data that can be stored in a queue. Once the upper limit is reached, the insertion causes blocking until the data in the queue is consumed.
#coding: Utf-8import queueq=queue.queue (5) #队列中只能存放5个数据for I in range (5): q.put (i) and not Q.empty (): print Q.get ()
LIFO queue: LIFO
Class Queue.lifoqueue (maxsize=0)
#coding: Utf-8import queueq=queue.lifoqueue (5) #队列中只能存放5个数据for I in range (5): q.put (i) and not Q.empty (): Print Q.get ()
Results:
4
3
2
1
0
Priority queue
Class Queue.priorityqueue (maxsize=0)
Import Queueimport Threadingclass Job (object): def __init__ (self, Priority, description): self.priority = Priority self.description = description print ' Job: ', description return def __cmp__ (self, Other): return CMP (self.priority, other.priority) q = Queue.priorityqueue () q.put (Job (3, ' Level 3 job ')) q.put (Job (10, ' Level Jobs ') q.put (Job (1, ' Level 1 job ')) def process_job (q): When True: next_job = Q.get () print ' for: ', Next_job.description q.task_done () workers = [Threading. Thread (Target=process_job, args= (Q,)), Threading. Thread (Target=process_job, args= (Q,)) ]for W in workers: W.setdaemon (True) W.start () Q.join ( )
Common methods:
Queue.qsize()
Queue.empty()
Queue.full()
Queue.put(item, block=true, timeout=none)
Queue.put_nowait(item)
Queue.get(block=true, timeout=none)
Queue.get_nowait()
Queue.task_done()
Queue.join()
Producer Consumer Model
Python queues queue