How to limit rate of requests to web services in Python?

后端 未结 6 1849
醉梦人生
醉梦人生 2020-12-30 06:13

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 l

6条回答
  •  时光说笑
    2020-12-30 06:29

    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.

    The Dispatch side does this.

    1. Get the request start time, s.

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

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

    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.

    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.

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

提交回复
热议问题