本文介绍了在Grequests任务中添加进度反馈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遵循了grequests 用法示例,但是我试图添加一些进度反馈.已完成请求的百分比.我该如何实现?
I've followed the grequests usage example, but I'm trying to add some progress feedback. A percentage of the completed requests. How could I achieve that?
import grequests urls = [ 'www.heroku', 'python-tablib', 'httpbin', 'python-requests', 'kennethreitz' ] 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) 推荐答案尝试一下:
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更多推荐
在Grequests任务中添加进度反馈
发布评论