python-3.x - python如何在不中断while循环的情况下引发异常
问题描述
假设我有一些事件监听器假设一直运行,并且有一些exceptions
我想将它们传递给函数调用者,就像这样
import asyncio
_ = 0
async def listener(loop):
while True:
await asyncio.sleep(0.5)
if _ != 0:
raise ValueError('the _ is not 0 anymore!')
print('okay')
async def executor(loop):
while True:
x = await loop.run_in_executor(None, input, 'execute: ')
global _
_ = x
async def main(loop):
asyncio.ensure_future(listener(loop), loop=loop)
await executor(loop)
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
如果您要更改该值,则侦听器事件循环将制动,但我不希望它break
发生raise
错误,因此您将能够捕获它并loop
继续前进
解决方案
我不希望它破坏我希望它引发错误,这样你就可以抓住它并且循环继续下去
如果您想提出错误但继续前进,那么侦听器不应该raise
直接。Future
相反,它应该向使用对象的感兴趣的各方发出异常信号。而不是等待执行者,main()
应该在循环中等待广播未来:
import asyncio
_ = 0
broadcast = None
async def listener():
while True:
await asyncio.sleep(0.5)
if _ != 0:
broadcast.set_exception(ValueError('the _ is not 0 anymore!'))
else:
print('okay')
async def executor():
loop = asyncio.get_event_loop()
while True:
x = int(await loop.run_in_executor(None, input, 'execute: '))
global _
_ = x
async def main():
global broadcast
loop = asyncio.get_event_loop()
loop.create_task(listener())
loop.create_task(executor())
while True:
broadcast = loop.create_future()
try:
await broadcast
except ValueError as e:
print('got error', e)
asyncio.get_event_loop().run_until_complete(main())
推荐阅读
- wordpress - 缩短我的网格 - 存档 - 页面的帖子标题网址
- node.js - SQLite3 Node.js JSON
- python - 如何在python中编程以在每个函数调用上自动增加一个数字
- c# - 快速移动图片框导致闪烁 C#
- elixir - Elixir: Idiomatic way to calculate difference of consecutive elements?
- php - 刷新后网页自动提交以前的会话变量
- neural-network - 不是卷积的神经网络如何称为
- elasticsearch - elasticsearch中的文件路径搜索
- c++ - 程序因“std::out_of_range”错误而崩溃
- c++ - C++ - Unicode 换行符