小编Jas*_*son的帖子

无法捕获 asyncio.TimeoutError?

我正在使用 asyncio 来获取 url,有时它们会超时,尽我所能,我无法使用以下代码捕获 asyncio.TimeoutError !

async def fetch(url, session):
    """Fetch a url, using specified ClientSession."""
    async with session.get(url) as response:
        # print(f"fetching {url}")
        try:
            resp = await response.read()
        except asyncio.TimeoutError:
            return {"results": f"timeout error on {url}"}

        if response.status != 200:
            return {"error": f"server returned {response.status}"}

        return str(resp, 'utf-8').rstrip()
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪。我能做些什么来捕捉这个异常并记录它而不是退出我的程序?

 resource: {…}  
 severity:  "ERROR"  
 textPayload:  "Traceback (most recent call last):
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 346, in run_http_function
    result = _function_handler.invoke_user_function(flask.request)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line …
Run Code Online (Sandbox Code Playgroud)

python exception python-asyncio

6
推荐指数
1
解决办法
4663
查看次数

标签 统计

exception ×1

python ×1

python-asyncio ×1