2015-11-14 104 views
0

我遵循grequests usage example,但我試圖添加一些進度反饋。已完成請求的百分比。我怎麼能做到這一點?在Grequests任務中添加進度反饋

import grequests 

urls = [ 
    'http://www.heroku.com', 
    'http://python-tablib.org', 
    'http://httpbin.org', 
    'http://python-requests.org', 
    'http://kennethreitz.com' 
] 

def feedback(r, **kwargs): 
      print "%s fetched." % r.url 
      return r 

rs = (grequests.get(u, callback=feedback) for u in urls) 
res = grequests.map(rs) 
+0

退房:http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console如何使反饋,但真正的問題是調用.map塊。 – domoarrigato

回答

1

試試這個:

from gevent import monkey 
monkey.patch_all() 
import gevent 
import sys 
import requests 

rs = [gevent.spawn(requests.get, u) for u in urls] 
[i.start() for i in rs] 
while 1: 
    gevent.sleep() 
    percent = 0.0 
    for i in rs: 
     if i.successful(): 
      percent += 100/len(rs) 
    sys.stdout.write(('='*int(percent))+(''*(100-int(percent)))+("\r [ %d"%percent+"% ] ")) 
    sys.stdout.flush() 
    if percent == 100: 
     sys.stdout.write('\n') 
     sys.stdout.flush() 
     break