在Grequests任务中添加进度反馈

时间:2015-11-14 00:17:08

标签: python concurrency gevent grequests

我已经关注了问候语usage example,但我正在尝试添加一些进度反馈。已完成请求的百分比。我怎么能做到这一点?

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)

1 个答案:

答案 0 :(得分: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