Skip to content

Commit

Permalink
fix[utils]超时重试
Browse files Browse the repository at this point in the history
  • Loading branch information
汉广 committed Jul 25, 2023
1 parent 774f097 commit 1c0aaeb
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ async def send_with_async(method: str, url: str, proxy: dict = None, **kwargs) -
'''
kwargs = get_default_args(**kwargs)
max_try_time = 3
try:
async with httpx.AsyncClient(proxies=proxy, follow_redirects=True, verify=False) as client:
req = await client.request(method, url, **kwargs)
return req
except TimeoutError:
max_try_time -= 1
if max_try_time < 0:
logger.error(
f"max_try_time(count={max_try_time}) exceeded to request in httpx({method} -> {url})")
while True:
try:
async with httpx.AsyncClient(proxies=proxy, follow_redirects=True, verify=False) as client:
req = await client.request(method, url, **kwargs)
return req
except TimeoutError:
max_try_time -= 1
if max_try_time > 0:
continue
msg = f"max_try_time(count={max_try_time}) exceeded to request in httpx({method} -> {url})"
logger.error(msg)
return None
except Exception as ex:
logger.error(f"fail to request in httpx({method} -> {url}):[{type(ex).__name__}]{ex}")
return None
except Exception as ex:
logger.error(f"fail to request in httpx({method} -> {url}):\n{ex}")
return None


async def get_url(url: str, proxy: dict = None, **kwargs) -> str:
Expand Down

0 comments on commit 1c0aaeb

Please sign in to comment.