返回 Social Auto Upload
network.py
根目录 / utils / network.py
1 import asyncio
2 import time
3 from functools import wraps
4
5
6 def async_retry(timeout=60, max_retries=None):
7 def decorator(func):
8 @wraps(func)
9 async def wrapper(*args, **kwargs):
10 start_time = time.time()
11 attempts = 0
12 while True:
13 try:
14 return await func(*args, **kwargs)
15 except Exception as e:
16 attempts += 1
17 if max_retries is not None and attempts >= max_retries:
18 print(f"Reached maximum retries of {max_retries}.")
19 raise Exception(f"Failed after {max_retries} retries.") from e
20 if time.time() - start_time > timeout:
21 print(f"Function timeout after {timeout} seconds.")
22 raise TimeoutError(f"Function execution exceeded {timeout} seconds timeout.") from e
23 print(f"Attempt {attempts} failed: {e}. Retrying...")
24 await asyncio.sleep(1) # Sleep to avoid tight loop or provide backoff logic here
25
26 return wrapper
27
28 return decorator
28 lines PYTHON