首页 > 解决方案 > 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继续前进

标签: python-3.xasynchronousexception-handlingpython-asyncio

解决方案


我不希望它破坏我希望它引发错误,这样你就可以抓住它并且循环继续下去

如果您想提出错误但继续前进,那么侦听器不应该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())

推荐阅读