在Grequests任务中添加进度反馈

编程入门 行业动态 更新时间:2024-10-09 03:23:45
本文介绍了在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任务中添加进度反馈

本文发布于:2023-11-26 09:26:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633557.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   反馈   Grequests

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!