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

A developer is writing an automation script that interacts with a Cisco API. To ensure the script can recover from temporary network glitches or API rate limiting, the developer wants to implement a retry mechanism with an exponential backoff strategy. Which of the following Python libraries is best suited for adding robust retry logic to HTTP requests?

  1. Ajson
  2. Btenacity
  3. Cos
  4. Drequests
Show answer & explanation

Correct answer: B. tenacity

The `tenacity` library is specifically designed to add retry capabilities to Python functions, including HTTP requests, with advanced features like exponential backoff, jitter, and custom retry conditions, making it ideal for robust automation scripts.

Why the other options are wrong

  • A. The `json` library is for encoding and decoding JSON data, unrelated to retry mechanisms.
  • C. The `os` library provides a way of using operating system dependent functionality, not HTTP retry logic.
  • D. The `requests` library handles making HTTP requests but does not inherently provide advanced retry logic like exponential backoff.

Python Tenacity Library

The `tenacity` library provides a general-purpose retry decorator for Python functions, enabling robust error handling with strategies like exponential backoff and jitter.

  • Decorator-based retry mechanism.
  • Supports exponential backoff, jitter.
  • Configurable retry conditions and stop strategies.

Memory trick: Tenacity helps you 'tenaciously' retry until success.

More Understanding and Using APIs questions