推荐学习书目
› 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 · Dec 8, 2022 · 3630 views
    This topic created in 1388 days ago, the information mentioned may be changed or developed.

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

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

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

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

    之前写了个测速脚本, 控制并发,但是每次都说一开始能跑满,慢慢就越来越低了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2610 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 09:44 · PVG 17:44 · LAX 02:44 · JFK 05:44
    ♥ Do have faith in what you're doing.