calss Queue.Queue(maxsize=0)
maxsize是个整数,指明了队列中能存放的数据个数的上限。一旦达到上限,插入会导致阻塞,直到队列中的数据被消费掉。
#coding:utf-8 import Queue q=Queue.Queue(5) #队列中只能存放5个数据 for i in range(5): q.put(i) while not q.empty(): print q.get()
class Queue.LifoQueue(maxsize=0)
#coding:utf-8 import Queue q=Queue.LifoQueue(5) #队列中只能存放5个数据 for i in range(5): q.put(i) while not q.empty(): print q.get()
结果:
4
3
2
1
0
class Queue.PriorityQueue(maxsize=0)
import Queue import threading class 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 10 job‘)) q.put(Job(1, ‘level 1 job‘)) def process_job(q): while 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()
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
()
原文:https://www.cnblogs.com/iexperience/p/9326279.html