首页 > 代码库 > gevent queue应用2 队列设置了最大数量限制

gevent queue应用2 队列设置了最大数量限制

 1 # -*- coding:utf-8 -*- 2 #! /usr/bin/env python 3 ‘‘‘ 4 Created on 2014年9月2日 5 ‘‘‘ 6 import gevent 7 from gevent.queue import Queue, Empty 8 import random 9 10 links = Queue(maxsize=5)11 12 def downloader(pThread):13     try:14         while True:15             link = links.get()16             gevent.sleep(random.randint(1,6)*0.001)17             print("%s: %s has been downloaded!" % (pThread,link))18     except Empty:19         print("Has no picture link in Queue! All done!")20         21 def getLinks():22     for i in xrange(1,500):23         links.put("http://baidu.com/%s.jpg" % i)24     print("Assigned all work in iteration 1")25 26 27 28 pThreads = [gevent.spawn(downloader,"pThread%s" % i) for i in xrange(10)]29 30 # gevent.spawn(getLinks).join()    #为何报错?31 # gevent.joinall(pThreads)32 33 gevent.joinall([gevent.spawn(getLinks)].extend(pThreads))

 

gevent queue应用2 队列设置了最大数量限制