限制 CGI 脚本中 API 的请求速率

发布于 2024-09-09 03:46:37 字数 120 浏览 2 评论 0原文

我正在编写的脚本有时会向 API 发出请求,而 API 要求将请求限制为每秒最多 1 个。

将 API 请求限制为每秒 1 次的最直接方法是什么?

每次发出请求时是否都会将当前时间存储在文件中?

The script that I'm writing sometimes makes requests to an API and the API requires that requests are limited to a maximum of 1 per second.

What is the most straight forward way of limiting my requests to the API to 1 every second?

Would it involve storing the current time in a file each time a request is made?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈年往事 2024-09-16 03:46:37

您可以为 CGI 调用使用单独的线程,并使用在每次迭代时调用睡眠循环的排队机制。

来自 15.3。时间

时间.睡眠(秒)
暂停执行指定的秒数。该参数可以是浮点数以指示更精确的睡眠时间。实际的挂起时间可能小于请求的时间,因为任何捕获的信号都将在执行该信号的捕获例程后终止 sleep() 。此外,由于系统中其他活动的调度,暂停时间可能比请求的时间长任意量。

You could use a separate thread for the CGI calls and a queuing mechanism that loops with a call to sleep on each iteration.

From 15.3. time

time.sleep(secs)
Suspend execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

寻梦旅人 2024-09-16 03:46:37

可以在函数一上使用速率限制 python 装饰器希望限制速率,就像 Greg Burek 的这个:

import time

def RateLimited(maxPerSecond):
    minInterval = 1.0 / float(maxPerSecond)
    def decorate(func):
        lastTimeCalled = [0.0]
        def rateLimitedFunction(*args,**kargs):
            elapsed = time.clock() - lastTimeCalled[0]
            leftToWait = minInterval - elapsed
            if leftToWait>0:
                time.sleep(leftToWait)
            ret = func(*args,**kargs)
            lastTimeCalled[0] = time.clock()
            return ret
        return rateLimitedFunction
    return decorate

@RateLimited(2)  # 2 per second at most
def PrintNumber(num):
    print num

if __name__ == "__main__":
    print "This should print 1,2,3... at about 2 per second."
    for i in range(1,100):
        PrintNumber(i)

One can use a rate-limiting python decorator on the function one wishes to rate-limit, like this one from Greg Burek:

import time

def RateLimited(maxPerSecond):
    minInterval = 1.0 / float(maxPerSecond)
    def decorate(func):
        lastTimeCalled = [0.0]
        def rateLimitedFunction(*args,**kargs):
            elapsed = time.clock() - lastTimeCalled[0]
            leftToWait = minInterval - elapsed
            if leftToWait>0:
                time.sleep(leftToWait)
            ret = func(*args,**kargs)
            lastTimeCalled[0] = time.clock()
            return ret
        return rateLimitedFunction
    return decorate

@RateLimited(2)  # 2 per second at most
def PrintNumber(num):
    print num

if __name__ == "__main__":
    print "This should print 1,2,3... at about 2 per second."
    for i in range(1,100):
        PrintNumber(i)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文