js代码改写py
This commit is contained in:
22
m2pool_backend_app/public/retry.py
Normal file
22
m2pool_backend_app/public/retry.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
async def execute_with_retry(task, max_retries, delay):
|
||||
attempts = 0
|
||||
|
||||
while attempts < max_retries:
|
||||
try:
|
||||
return await task()
|
||||
except Exception as error:
|
||||
attempts += 1
|
||||
print(f"尝试 {attempts} 失败: {error}", file=sys.stderr)
|
||||
|
||||
if attempts >= max_retries:
|
||||
print("已达最大重试次数,任务失败。", file=sys.stderr)
|
||||
raise
|
||||
|
||||
print(f"等待 {delay} 秒后重试...")
|
||||
await asyncio.sleep(delay)
|
||||
|
||||
|
||||
__all__ = ["execute_with_retry"]
|
||||
Reference in New Issue
Block a user