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))
相關文章
相關標籤/搜索