मुख्य सामग्री पर जाएँ
Skillpedia
API और सर्वर (backend)41k · #6766

python-resilience

यह क्या करती है

यह टूल कंप्यूटर प्रोग्राम को ज़्यादा भरोसेमंद बनाने में मदद करता है। अगर नेटवर्क जैसी कोई चीज़ अस्थायी रूप से गड़बड़ हो जाए, तो प्रोग्राम अपने आप दोबारा कोशिश करता है और दोबारा कोशिश करने से पहले थोड़ा रुकता है।

कब इस्तेमाल करें

  • ✓जब आपके प्रोग्राम का कोई हिस्सा किसी दूसरी सर्विस से जुड़ना चाहता हो और उसके फेल होने का खतरा हो।
  • ✓अगर आप चाहते हैं कि छोटी-मोटी अस्थायी गड़बड़ियों के बावजूद आपका प्रोग्राम चलता रहे।
  • ✓धीरे-धीरे दोबारा कोशिश करके किसी परेशान सर्विस पर ज़्यादा बोझ डालने से बचने के लिए।

Skill का लिंक

https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-resilience/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-resilience/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“अगर वेबसाइट कनेक्शन फेल हो जाए, तो मेरे प्रोग्राम को हर बार थोड़ा ज़्यादा इंतज़ार करके अधिकतम 3 बार दोबारा कोशिश करने दो।”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

अंदर से कोड 'retry' नाम की तकनीक इस्तेमाल करता है। अगर कोई ऑपरेशन फेल होता है, तो वह हर कोशिश के साथ बढ़ते हुए समय तक रुकता है (exponential backoff) और फिर दोबारा कोशिश करता है। इससे प्रोग्राम क्रैश होने से बच जाता है।

तकनीकी सामग्री (मूल SKILL.md)

लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।

Python Resilience Patterns

Build fault-tolerant Python applications that gracefully handle transient failures, network issues, and service outages. Resilience patterns keep systems running when dependencies are unreliable.

When to Use This Skill

  • Adding retry logic to external service calls
  • Implementing timeouts for network operations
  • Building fault-tolerant microservices
  • Handling rate limiting and backpressure
  • Creating infrastructure decorators
  • Designing circuit breakers

Core Concepts

1. Transient vs Permanent Failures

Retry transient errors (network timeouts, temporary service issues). Don't retry permanent errors (invalid credentials, bad requests).

2. Exponential Backoff

Increase wait time between retries to avoid overwhelming recovering services.

3. Jitter

Add randomness to backoff to prevent thundering herd when many clients retry simultaneously.

4. Bounded Retries

Cap both attempt count and total duration to prevent infinite retry loops.

Quick Start

from tenacity import retry, stop_after_attempt, wait_exponential_jitter

@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential_jitter(initial=1, max=10),
)
def call_external_service(request: dict) -> dict:
    return httpx.post("https://api.example.com", json=request).json()

Fundamental Patterns

Pattern 1: Basic Retry with Tenacity

Use the tenacity library for production-grade retry logic. For simpler cases, consider built-in retry functionality or a lightweight custom implementation.

from tenacity import (
    retry,
    stop_after_attempt,
    stop_after_delay,
    wait_exponential_jitter,
    retry_if_exception_type,
)

TRANSIENT_ERRORS = (ConnectionError, TimeoutError, OSError)

@retry(
    retry=retry_if_exception_type(TRANSIENT_ERRORS),
    stop=stop_after_attempt(5) | stop_after_delay(60),
    wait=wait_exponential_jitter(initial=1, max=30),
)
def fetch_data(url: str) -> dict:
    """Fetch data with automatic retry on transient failures."""
    response = httpx.get(url, timeout=30)
    response.raise_for_status()
    return response.json()

Pattern 2: Retry Only Appropriate Errors

Whitelist specific transient exceptions. Never retry:

  • ValueError, TypeError - These are bugs, not transient issues
  • AuthenticationError - Invalid credentials won't become valid
  • HTTP 4xx errors (except 429) - Client errors are permanent
from tenacity import retry, retry_if_exception_type
import httpx

# Define what's retryable
RETRYABLE_EXCEPTIONS = (
    ConnectionError,
    TimeoutError,
    httpx.ConnectTimeout,
    httpx.ReadTimeout,
)

@retry(
    retry=retry_if_exception_type(RETRYABLE_EXCEPTIONS),
    stop=stop_after_attempt(3),
    wait=wait_exponential_jitter(initial=1, max=10),
)
def resilient_api_call(endpoint: str) -> dict:
    """Make API call with retry on network issues."""
    return httpx.get(endpoint, timeout=10).json()

Pattern 3: HTTP Status Code Retries

Retry specific HTTP status codes that indicate transient issues.

from tenacity import retry, retry_if_result, stop_after_attempt
import httpx

RETRY_STATUS_CODES = {429, 502, 503, 504}

def should_retry_response(response: httpx.Response) -> bool:
    """Check if response indicates a retryable error."""
    return response.status_code in RETRY_STATUS_CODES

@retry(
    retry=retry_if_result(should_retry_response),
    stop=stop_after_attempt(3),
    wait=wait_exponential_jitter(initial=1, max=10),
)
def http_request(method: str, url: str, **kwargs) -> httpx.Response:
    """Make HTTP request with retry on transient status codes."""
    return httpx.request(method, url, timeout=30, **kwargs)

Pattern 4: Combined Exception and Status Retry

Handle both network exceptions and HTTP status codes.

from tenacity import (
    retry,
    retry_if_exception_type,
    retry_if_result,
    stop_after_attempt,
    wait_exponential_jitter,
    before_sleep_log,
)
import logging
import httpx

logger = logging.getLogger(__name__)

TRANSIENT_EXCEPTIONS = (
    ConnectionError,
    TimeoutError,
    httpx.ConnectError,
    httpx.ReadTimeout,
)
RETRY_STATUS_CODES = {429, 500, 502, 503, 504}

def is_retryable_response(response: httpx.Response) -> bool:
    return response.status_code in RETRY_STATUS_CODES

@retry(
    retry=(
        retry_if_exception_type(TRANSIENT_EXCEPTIONS) |
        retry_if_result(is_retryable_response)
    ),

…
अक्सर पूछे जाने वाले सवाल

अस्थायी फेल होना (transient failures) क्या होता है?

ये ऐसी समस्याएँ हैं जो अपने आप ठीक हो जाती हैं, जैसे नेटवर्क का कुछ देर के लिए धीमा होना या किसी सर्विस का कुछ समय बंद रहकर फिर चालू हो जाना।

हर बार दोबारा कोशिश में ज़्यादा इंतज़ार क्यों?

ताकि फेल हो रही सर्विस को ठीक होने का समय मिले। बहुत जल्दी-जल्दी कई बार कोशिश करने से समस्या और बढ़ सकती है।

'jitter' क्या है?

इसमें इंतज़ार के समय में थोड़ा सा रैंडम समय जोड़ा जाता है। इससे कई प्रोग्राम ठीक एक ही पल में दोबारा कोशिश नहीं करते, और सर्विस पर अचानक बोझ नहीं पड़ता।

जानकारी

लेखक का मूल विवरण

Python resilience patterns including automatic retries, exponential backoff, timeouts, and fault-tolerant decorators. Use when adding retry logic, implementing timeouts, building fault-tolerant services, or handling transient failures.

repo: wshobson/agents

लोकप्रियता

GitHub स्टार
41k
रैंकिंग में स्थान
#6766
लाइसेंस
MIT
अपडेट की गई
15/10/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

मिलती-जुलती skills