Ask Your Question
1

What is the proper method for making multiple API requests from a list, with a maximum rate of two requests per second?

asked 2023-07-15 00:33:21 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-15 00:36:02 +0000

djk gravatar image

The proper method for making multiple API requests from a list, with a maximum rate of two requests per second, is to use a queue system that allows you to schedule and execute requests at a controlled rate. Here's a high-level approach:

  1. Define a queue data structure to hold the list of API endpoints to call, and a counter to keep track of the number of requests made so far.
  2. Configure a timer that triggers every half-second (or slower if preferred, depending on the maximum rate), or use a sleep function to pause execution in between requests.
  3. In the loop or timer, check the queue to see if there are any requests to make. If there are, dequeue the next item.
  4. Before making the API request, check the request counter to see if the maximum rate of two requests per second has been reached. If not, increment the counter.
  5. Make the API request using a library or function that supports asynchronous or non-blocking I/O, so that the execution of the code doesn't block the timer loop. Save the result in a data cache or file.
  6. After the API request is complete, decrement the counter, and loop again to process the next item in the queue.

This approach ensures that calls are made at a controlled rate, preventing the API provider from blocking your requests or throttling your access. It also allows you to efficiently process the list without exceeding its limits.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-15 00:33:21 +0000

Seen: 12 times

Last updated: Jul 15 '23