使用 aiohttp 重試異步 API 呼叫的最佳方法是什么?我想重試標準套接字錯誤、超時錯誤等以及某些狀態代碼的請求500, 501。我曾嘗試使用aiohttp_async,但無法讓它作業:
import asyncio
from aiohttp import ClientSession
from aiohttp_retry import RetryClient
# Async single retry fetch
async def async_retry_fetch(url, retry_client):
async with retry_client.get(url, retry_attempts=3, retry_for_status=[500, 501]) as response:
try:
data = await response.json()
except Exception as e:
raise Exception("Could not convert json")
return data
async def main():
urls = [
"https://httpstat.us/200",
"https://httpstat.us/500"
]
api_calls = []
async with ClientSession() as session:
retry_client = RetryClient(session)
for url in urls:
api_calls.append(async_retry_fetch(url, retry_client))
res = await asyncio.gather(*api_calls, return_exceptions=True)
print("RESULT", res)
asyncio.run(main())
輸出:
RESULT [AttributeError("'ClientSession' object has no attribute 'debug'"), AttributeError("'ClientSession' object has no attribute 'debug'")]
uj5u.com熱心網友回復:
看起來您已經安裝了aiohttp_retry 2.x 版
,但您使用了1.2 版的引數,這給出了AttributeError.
舊版本可以使用
get(..., retry_attempts=3)
但新版本需要
get(..., retry_options=ExponentialRetry(attempts=3)
要么
RetryClient(..., retry_options=ExponentialRetry(attempts=3))
但是attemptsinRetryClient具有默認值3,因此您可以跳過它。
另一個問題是RetryClient()不能session作為引數。
它不需要它,因為它創建了自己ClientSession()的__init__
查看源代碼
這對我有用:
import asyncio
from aiohttp_retry import RetryClient, ExponentialRetry
class MyLogger():
def debug(self, *args, **kwargs):
print('[debug]:', *args, **kwargs)
async def async_retry_fetch(url, retry_client):
retry_options = ExponentialRetry(attempts=3)
#async with retry_client.get(url) as response:
# OR
async with retry_client.get(url, retry_options=ExponentialRetry(attempts=3), raise_for_status=[500, 501]) as response:
try:
data = await response.json()
except Exception as e:
raise Exception("Could not convert json")
return data
async def main():
urls = [
"https://httpstat.us/200",
"https://httpstat.us/500",
"https://httpstat.us/501",
"https://httpbin.org/status/500",
"https://httpbin.org/status/501",
"https://httpbin.org/json"
]
#async with RetryClient(logger=MyLogger(), retry_options=ExponentialRetry(attempts=3), raise_for_status=[500, 501]) as retry_client:
# OR
async with RetryClient(logger=MyLogger()) as retry_client:
api_calls = []
for url in urls:
api_calls.append(async_retry_fetch(url, retry_client))
res = await asyncio.gather(*api_calls, return_exceptions=True)
for item in res:
print(item)
print('---')
# --- start ---
asyncio.run(main())
結果:
[debug]: Attempt 0 out of 3
[debug]: Attempt 0 out of 3
[debug]: Attempt 0 out of 3
[debug]: Attempt 0 out of 3
[debug]: Attempt 0 out of 3
[debug]: Attempt 0 out of 3
[debug]: Attempt 1 out of 3
[debug]: Attempt 1 out of 3
[debug]: Attempt 1 out of 3
[debug]: Attempt 1 out of 3
[debug]: Attempt 2 out of 3
[debug]: Attempt 2 out of 3
[debug]: Attempt 2 out of 3
[debug]: Attempt 2 out of 3
Could not convert json
---
500, message='Internal Server Error', url=URL('https://httpstat.us/500')
---
501, message='Not Implemented', url=URL('https://httpstat.us/501')
---
500, message='INTERNAL SERVER ERROR', url=URL('https://httpbin.org/status/500')
---
501, message='NOT IMPLEMENTED', url=URL('https://httpbin.org/status/501')
---
{'slideshow': {'author': 'Yours Truly', 'date': 'date of publication', 'slides': [{'title': 'Wake up to WonderWidgets!', 'type': 'all'}, {'items': ['Why <em>WonderWidgets</em> are great', 'Who <em>buys</em> WonderWidgets'], 'title': 'Overview', 'type': 'all'}], 'title': 'Sample Slide Show'}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442715.html
上一篇:將html加載到chrome中,然后加載jsdom實體
下一篇:如何獲取大型子域串列的回應狀態?
