推荐学习书目
› 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
biuyixia
V2EX  ›  Python

大佬们,怎么控制本地请求 API 的频率?

  •  
  •   biuyixia · Oct 14, 2022 · 3487 views
    This topic created in 1443 days ago, the information mentioned may be changed or developed.

    某 API 服务限制每个 IP 每秒最多 5 个请求

    某 IP 服务器上有多个 Python 文件,这几个文件运行后都有可能去请求这个 API ,有时候每秒会超过 5 次。

    大佬们,有什么好方法能从自己服务器上控制这个请求频率呢,要是一个 Py 文件还好说,关键多个 Py 文件。

    谢谢。

    5 replies  •  2022-10-15 18:22:45 +08:00
    coderxy
        1
    coderxy  
       Oct 14, 2022   ❤️ 1
    用 redis 或者数据库做计数,秒级限流
    nulIptr
        2
    nulIptr  
       Oct 14, 2022
    感觉这个需求很奇怪,八成是 xy 问题
    不过实现也很容易,搞个本地的代理服务器来限流就行了啊。流量先打到你的代理,限流之后再发给真正的 api
    S1ngleton
        3
    S1ngleton  
       Oct 15, 2022 via iPhone
    起一个服务,发放令牌,每个 py 请求前先去获取,获取到则请求
    tt7
        4
    tt7  
       Oct 15, 2022
    用一个带 rate limit 的 local http proxy

    比如 https://github.com/Clever/sphinx
    zyxbcde
        5
    zyxbcde  
       Oct 15, 2022 via iPhone
    这题我会,我遇到过。
    async with Lock:
    Asyncio.sleep(0.25)
    Session.get()
    我本地起了一个后端自测请求确实是间隔 0.25 秒发过来的。不过还需要注意如果接口慢叠加最大连接数影响,还有网络延迟之类的。你一秒发出去 5 个请求,到服务器时候可能一秒 4 个也可能有 6 个...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   837 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 21:36 · PVG 05:36 · LAX 14:36 · JFK 17:36
    ♥ Do have faith in what you're doing.