本文介绍了在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 = [
'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)
推荐答案
尝试一下:
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任务中添加进度反馈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!