gpt4 book ai didi

python - 如何在 Python 中使用 eventlet 引发异常?

转载 作者:太空宇宙 更新时间:2023-11-03 14:13:35 25 4
gpt4 key购买 nike

我有一个简单的代码:

导入事件

def execute():
print("Start")
timeout = Timeout(3)
try:
print("First")
sleep(4)
print("Second")
except:
raise TimeoutException("Error")
finally:
timeout.cancel()
print("Third")

此代码应抛出 TimeoutException,因为“try”中的代码会阻止执行超过 3 秒。但是这个异常在循环中变浅了。我在输出中看不到它

这是输出:

Start
First

Process finished with exit code 0

如何在输出中引发此异常?

最佳答案

将 sleep(4) 改为

eventlet.sleep(4)

关于python - 如何在 Python 中使用 eventlet 引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35200291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com