如何在 Python 中限制对 Web 服务的请求率? [英] How to limit rate of requests to web services in Python?

查看:42
本文介绍了如何在 Python 中限制对 Web 服务的请求率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个与 Web 服务 API 接口的 Python 库.像我遇到的许多 Web 服务一样,这个请求限制了请求的速率.我想为类实例化提供一个可选参数 limit,如果提供,它将保持传出请求,直到指定的秒数过去.

I'm working on a Python library that interfaces with a web service API. Like many web services I've encountered, this one requests limiting the rate of requests. I would like to provide an optional parameter, limit, to the class instantiation that, if provided, will hold outgoing requests until the number of seconds specified passes.

我了解一般情况如下:类的实例通过方法发出请求.当它这样做时,该方法会发出一些信号,在某处设置一个锁定变量,并开始一个倒数计时器,用于limit 中的秒数.(很可能,锁是倒数计时器本身.)如果在此时间范围内发出另一个请求,则必须排队,直到倒数计时器达到零并且锁被解除;此时,队列中最早的请求被发送,并且倒数计时器被重置并重新锁定.

I understand that the general scenario is the following: an instance of the class makes a request via a method. When it does, the method emits some signal that sets a lock variable somewhere, and begins a countdown timer for the number of seconds in limit. (In all likelihood, the lock is the countdown timer itself.) If another request is made within this time frame, it must be queued until the countdown timer reaches zero and the lock is disengaged; at this point, the oldest request on the queue is sent, and the countdown timer is reset and the lock is re-engaged.

这是线程的情况吗?还有其他我没有看到的方法吗?

Is this a case for threading? Is there another approach I'm not seeing?

倒数计时器和锁应该是实例变量,还是应该属于该类,以便该类的所有实例都持有请求?

Should the countdown timer and lock be instance variables, or should they belong to the class, such that all instances of the class hold requests?

此外,在库中提供速率限制功能通常是一个坏主意吗?我的理由是,默认情况下,倒计时为零秒,该库仍然允许开发人员使用该库并提供他们自己的速率限制方案.考虑到任何使用该服务的开发人员无论如何都需要对请求进行速率限制,我认为该库提供一种速率限制方法会很方便.

Also, is this generally a bad idea to provide rate-limiting functionality within a library? I reason since, by default, the countdown is zero seconds, the library still allows developers to use the library and provide their own rate-limiting schemes. Given any developers using the service will need to rate-limit requests anyway, however, I figure that it would be a convenience for the library to provide a means of rate-limiting.

无论是否在库中放置速率限制方案,我都想使用该库编写应用程序,因此建议的技术将派上用场.

Regardless of placing a rate-limiting scheme in the library or not, I'll want to write an application using the library, so suggested techniques will come in handy.

推荐答案

使用队列和调度程序时效果更好.

This works out better with a queue and a dispatcher.

您将处理分为两个方面:来源调度.这些可以是单独的线程(或者单独的进程,如果这样更容易的话).

You split your processing into two sides: source and dispatch. These can be separate threads (or separate processes if that's easier).

端以任何让他们满意的速度创建和排队请求.

The Source side creates and enqueues requests at whatever rate makes them happy.

调度方会这样做.

  1. 获取请求开始时间,s.

出列请求,通过远程服务处理请求.

Dequeues a request, process the request through the remote service.

获取当前时间,t.睡眠 rate - (t - s) 秒.

Get the current time, t. Sleep for rate - (t - s) seconds.

如果您想运行直接连接到远程服务的 Source 端,您可以这样做,并绕过速率限制.这对于使用远程服务的模拟版本进行内部测试很有用.

If you want to run the Source side connected directly to the remote service, you can do that, and bypass rate limiting. This is good for internal testing with a mock version of the remote service.

关于此的困难部分是为您可以排队的每个请求创建一些表示.由于 Python 队列 几乎可以处理任何事情,因此您不必做太多事情.

The hard part about this is creating some representation for each request that you can enqueue. Since the Python Queue will handle almost anything, you don't have to do much.

如果您使用多处理,则必须pickle 将您的物品放入管道中.

If you're using multi-processing, you'll have to pickle your objects to put them into a pipe.

这篇关于如何在 Python 中限制对 Web 服务的请求率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆