【问题标题】:try/except not catching future's TimeoutError尝试/除了没有捕捉到未来的 TimeoutError
【发布时间】:2018-01-15 11:57:26
【问题描述】:

有人可以向我解释这里发生了什么吗? 我想在超时错误中添加一条消息:

future = asyncio.run_coroutine_threadsafe(do_stuff(), loop=loop)
try:
    return future.result(timeout=3)
except TimeoutError:
    raise TimeoutError("Time out occurred while doing stuff")

future.results() (concurrent.futures._base.py) 看起来像这样:

def result(self, timeout=None):
    # bla bla bla
    else:
        raise TimeoutError()

但是当超时发生时,我的 try/except 子句没有捕捉到超时。其实

future = asyncio.run_coroutine_threadsafe(do_stuff(), loop=loop)
try:
    return future.result(timeout=3)
except Exception as e:
    log.error(str(e))

显示空的e。 WTH?

【问题讨论】:

  • empty e:因为 TimeoutError 是在没有任何参数 TimeoutError() 的情况下创建的。真的不需要它们,它出错的事实足以表明出了什么问题。

标签: error-handling timeout python-3.6 python-asyncio try-except


【解决方案1】:

好的。我知道。我应该使用except asyncio.TimeoutError:,而不是基础TimeoutError

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多