Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带aexit的键盘输入___Python_Async Await_Python Asyncio - Fatal编程技术网

Python 带aexit的键盘输入__

Python 带aexit的键盘输入__,python,async-await,python-asyncio,Python,Async Await,Python Asyncio,_uuuaenter_uuu和uuu aexit_uuu方法旨在确保清理, 但是在抛出键盘中断后,不再调用uu aexit_u_u方法。 我可以想象这是因为“with”语句不是当前执行点的stacktrace的一部分(胡乱猜测)。 如何在asyncio中有效地处理这个问题 编辑: 我创建了一些示例代码: import asyncio class manager: async def __aenter__(self): print("enter") async

_uuuaenter_uuu和uuu aexit_uuu方法旨在确保清理, 但是在抛出键盘中断后,不再调用uu aexit_u_u方法。 我可以想象这是因为“with”语句不是当前执行点的stacktrace的一部分(胡乱猜测)。 如何在asyncio中有效地处理这个问题

编辑: 我创建了一些示例代码:

import asyncio

class manager:
    async def __aenter__(self):
        print("enter")


    async def __aexit__(self, exc_type, exc_val, exc_tb):
        print("exit")

async def func1():
    print("func1")
    async with manager():
        await asyncio.sleep(1000)

async def func2():
    print("func2")
    await asyncio.sleep(2)
    raise KeyboardInterrupt

loop = asyncio.get_event_loop()
loop.create_task(func1())
loop.create_task(func2())
loop.run_forever()

你所有的任务都还在,你只需要决定如何处理它们。如果您有行为良好的任务,那么它应该像取消它们一样简单,然后让它们运行直到完成

例如

产生:

func1
enter
func2
exit
Traceback (most recent call last):
  File "C:\Users\User\Documents\python\asyncio_test.py", line 26, in <module>
    loop.run_forever()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\base_events.py", line 295, in run_forever
    self._run_once()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\base_events.py", line 1254, in _run_once
    handle._run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\events.py", line 125, in _run
    self._callback(*self._args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 301, in _wakeup
    self._step()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 239, in _step
    result = coro.send(None)
  File "C:\Users\User\Documents\python\asyncio_test.py", line 19, in func2
    raise KeyboardInterrupt
KeyboardInterrupt

是什么让你认为
\uuuu aexit\uuu
没有被调用?我使用打印调用运行了一个简短的测试,结果表明,
\uuu aexit\uuu
确实在Python3.5I中被调用。我在aexit中添加了一条调试消息,但它没有被打印出来(Python3.7)@JosephGrootKormelink您无法用aexit捕获键盘中断,但是,与生成器类似,当对协同程序对象进行垃圾收集时,必须抛出GeneratorExit。我相信这是异步错误。在那里存档:你有多担心你会有一个行为不端的任务,在有机会时拒绝关闭?(例如,除了在一个while循环中之外,有一个裸环的东西)。如果您不担心,那么有一个相当简单的解决方案,否则实际上没有任何答案。
func1
enter
func2
exit
Traceback (most recent call last):
  File "C:\Users\User\Documents\python\asyncio_test.py", line 26, in <module>
    loop.run_forever()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\base_events.py", line 295, in run_forever
    self._run_once()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\base_events.py", line 1254, in _run_once
    handle._run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\events.py", line 125, in _run
    self._callback(*self._args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 301, in _wakeup
    self._step()
  File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 239, in _step
    result = coro.send(None)
  File "C:\Users\User\Documents\python\asyncio_test.py", line 19, in func2
    raise KeyboardInterrupt
KeyboardInterrupt
async def bad():
    while True:
        try:
            print('bad loop')
            await asyncio.sleep(0.5, loop=loop)
        except BaseException as e:
            print(repr(e))