DevNet Associate (DEVASC) v1.0Understanding and Using APIsHard

A network engineer is troubleshooting an issue where a Python script using `requests` to interact with a Cisco API intermittently fails with a `ConnectionError`. The API is known to be stable, and other tools like Postman can reach it consistently. The engineer suspects that the script is making too many requests in a short period, exceeding the API's limits. How can the engineer programmatically implement a delay between API calls to mitigate potential rate limiting issues?

  1. AUse asynchronous `asyncio` for simultaneous requests.
  2. BAdd `time.sleep(1)` after each `requests` call.
  3. CImplement a `try-except` block around `requests` calls.
  4. DIncrease the `timeout` parameter in the `requests` call.
Show answer & explanation

Correct answer: B. Add `time.sleep(1)` after each `requests` call.

The `time.sleep()` function in Python pauses the execution of the current thread for a specified number of seconds. Adding `time.sleep(1)` after each API call introduces a delay, which can effectively prevent the script from exceeding API rate limits by spacing out requests.

Why the other options are wrong

  • A. Asynchronous `asyncio` allows for concurrent operations, potentially *increasing* the rate of requests if not carefully managed, which would worsen rate limiting issues.
  • C. `try-except` blocks handle exceptions but don't introduce delays to prevent them; they react to errors, not proactively prevent rate limiting.
  • D. Increasing the `timeout` parameter makes the script wait longer for a response from the server, but it does not introduce a delay *between* requests to prevent hitting rate limits.

API Rate Limiting Mitigation (Delay)

To mitigate API rate limiting, applications can introduce deliberate delays between API calls using functions like `time.sleep()` to ensure they do not exceed the allowed request frequency, thus preventing HTTP 429 errors.

  • APIs enforce limits on requests per unit of time.
  • Exceeding limits results in HTTP 429 Too Many Requests.
  • Delays (e.g., `time.sleep()`) space out requests.
  • Exponential backoff is a more advanced strategy for retries.

Memory trick: Sleep to Slow, Backoff to Recover.

More Understanding and Using APIs questions