V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
monetto
V2EX  ›  Python

Python requests 库 限制请求速率

  •  1
     
  •   monetto · 2022-12-08 18:27:52 +08:00 · 2687 次点击
    这是一个创建于 498 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,项目中使用了 Python 的 requests 库,但是在某些场景下(例如 低带宽模式,有两个并行的 requests http 请求,一个很重要,一个优先级低一些),需要对,多个 requests 请求中的一个进行限速处理 ...

    V 友 有什么比较好的思路吗~

    5 条回复    2022-12-10 18:46:08 +08:00
    itskingname
        1
    itskingname  
       2022-12-08 18:47:31 +08:00   ❤️ 1
    requests 本身没有这个功能,但是你可以使用 walus 配合 Redis 实现。代码非常简单,就 3 行: https://walrus.readthedocs.io/en/latest/rate-limit.html
    monetto
        2
    monetto  
    OP
       2022-12-08 18:57:31 +08:00
    @itskingname 感谢,等有时间研究一下这个东西的实现思路是啥~

    目前我还能想到一种方案是 requests 利用多进程去做,然后通过 Linux 的一些网络工具去限制这个进程~
    edis0n0
        3
    edis0n0  
       2022-12-08 19:11:10 +08:00
    @itskingname #1 有相同的需求,因为我的爬虫需要同时发几万个请求所以用的是效率更高的 aiohttp ,经常把服务器带宽跑满了,导致大量请求超时,想要实现根据服务器带宽和请求内容大小自动调整请求频率,也可以这么做吗
    itskingname
        4
    itskingname  
       2022-12-08 20:14:59 +08:00
    @edis0n0 两个方案,使用 walus 是最简单的方案,只要 3 行代码。也有第二个方法,使用 asyncio 自带的 Semaphore
    levenwindy
        5
    levenwindy  
       2022-12-10 18:46:08 +08:00 via Android
    @itskingname
    我是纯 shell curl/wget 测速 能否实现一直跑满带宽?

    之前写了个测速脚本, 控制并发,但是每次都说一开始能跑满,慢慢就越来越低了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1018 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:58 · PVG 06:58 · LAX 15:58 · JFK 18:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.