python - 即使在循环停止后,run_until_complete 也会失败
问题描述
我正在尝试编写一个 SIGTERM 处理程序,它将具有我的run_forever() -loop
- 停止接受新任务。
- 完成跑步任务。
- 关闭。
这是我写的一个学习演示:
import asyncio
import signal
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s [%(name)s]: %(message)s', datefmt='%H:%M:%S')
_log = logging.getLogger(__name__)
class Looper:
def __init__(self, loop):
self._loop = loop
self._shutdown = False
signal.signal(signal.SIGINT, self._exit)
signal.signal(signal.SIGTERM, self._exit)
def _exit(self, sig, frame):
name = signal.Signals(sig).name
_log.info(f"Received shutdown-signal: {sig} ({name})")
self._shutdown = True
self._loop.stop() # << Stopping the event loop here.
_log.info(f"Loop stop initiated.")
pending = asyncio.all_tasks(loop=self._loop)
_log.info(f"Collected {len(pending)} tasks that have been stopped.")
if pending:
_log.info("Attempting to gather pending tasks: " + str(pending))
gatherer_set = asyncio.gather(*pending, loop=self._loop)
# self._loop.run_until_complete(gatherer_set) # << "RuntimeError: This event loop is already running"
_log.info("Shutting down for good.")
async def thumper(self, id, t):
print(f"{id}: Winding up...")
while not self._shutdown:
await asyncio.sleep(t)
print(f'{id}: Thump!')
print(f'{id}: Thud.')
loop = asyncio.get_event_loop()
lp = Looper(loop)
loop.create_task(lp.thumper('North Hall', 2))
loop.create_task(lp.thumper('South Hall', 3))
loop.run_forever()
_log.info("Done.")
在 Windows 10 和 Debian 10 上,上面的脚本都会对 SIGINT 做出反应并产生输出
North Hall: Winding up...
South Hall: Winding up...
North Hall: Thump!
South Hall: Thump!
North Hall: Thump!
South Hall: Thump!
North Hall: Thump!
09:55:53 INFO [__main__]: Received shutdown-signal: 2 (SIGINT)
09:55:53 INFO [__main__]: Loop stop initiated.
09:55:53 INFO [__main__]: Collected 2 tasks that have been stopped.
09:55:53 INFO [__main__]: Attempting to gather pending tasks: {<Task pending coro=<Looper.thumper() running at amazing_grace.py:42> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x02F91BF0>()]>>, <Task pending coro=<Looper.thumper() running at amazing_grace.py:42> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x02F91C10>()]>>}
09:55:53 INFO [__main__]: Shutting down for good.
09:55:53 INFO [__main__]: Done.
遗憾的是,表示thumper(..)演示调用实际上已经结束的“Thud.”行不会显示。我想,这是因为“聚集”只是给我带来了一组未实现的未来。但是,如果我敢激活run_until_complete() - 行,即使它位于self._loop.stop()之后,输出结束如下:
[...]
10:24:25 INFO [__main__]: Collected 2 tasks that have been stopped.
10:24:25 INFO [__main__]: Attempting to gather pending tasks: {<Task pending coro=<Looper.thumper() running at amazing_grace.py:41> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x03E417D0>()]>>, <Task pending coro=<Looper.thumper() running at amazing_grace.py:41> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x03E41BF0>()]>>}
Traceback (most recent call last):
File "amazing_grace.py", line 50, in <module>
loop.run_forever()
File "C:\Python37\lib\asyncio\base_events.py", line 539, in run_forever
self._run_once()
File "C:\Python37\lib\asyncio\base_events.py", line 1739, in _run_once
event_list = self._selector.select(timeout)
File "C:\Python37\lib\selectors.py", line 323, in select
r, w, _ = self._select(self._readers, self._writers, [], timeout)
File "C:\Python37\lib\selectors.py", line 314, in _select
r, w, x = select.select(r, w, w, timeout)
File "amazing_grace.py", line 35, in _exit
self._loop.run_until_complete(gatherer_set) # << "This event loop is already running"
File "C:\Python37\lib\asyncio\base_events.py", line 571, in run_until_complete
self.run_forever()
File "C:\Python37\lib\asyncio\base_events.py", line 526, in run_forever
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
问题归结为
- 在这种情况下如何调用或替换run_until_complete(..) ,以及
- 为什么我在停止循环后看到这个“循环正在运行”-错误。
该程序应在Windows 10和Linux下的Python 3.7上运行。
几天后编辑
正如 zaquest 在他/她的回答中所说的那样,当只是分配一个信号处理程序并create_task
在其中添加一个调用时,就会自找麻烦。正如我所观察到的,该例程可能会运行也可能不会运行(即使没有其他任务)。所以现在我添加了一个sys.platform
检查,看看脚本是否在 UNIX () 下运行。如果是这样,我更喜欢更可靠loop.add_signal_handler
地定义回调函数,这是我真正需要的。幸运的是 UNIX 是我的主要用例。主线:
self._loop.add_signal_handler(signal.signal(signal.SIGINT, self._exit, signal.SIGINT, None)
为什么要检查平台?:按照文档https://docs.python.org/3/library/asyncio-eventloop.html#unix-signals,loop.add_signal_handler() 在 Windows 上不可用,这不是真的惊讶地认为有问题的信号是 UNIX 术语。
解决方案
Python 信号处理程序在主线程中执行,在循环运行的同一线程中。BaseEventLoop.stop()
方法不会立即停止循环,而只是设置一个标志,这样当你的循环下次运行时,它只会执行已经安排好的回调,并且不再安排任何回调(参见run_forever)。但是,在您的信号处理程序返回之前,无法运行循环。这意味着您不能等到循环在信号处理程序中停止。相反,您可以安排另一个任务,该任务将等待您长时间运行的任务对更改做出反应,self._shutdown
然后停止循环。
class Looper:
...
def _exit(self, sig, frame):
name = signal.Signals(sig).name
_log.info("Received shutdown-signal: %s (%s)", sig, name)
self._shutdown = True
pending = asyncio.all_tasks(loop=self._loop)
_log.info("Attempting to gather pending tasks: " + str(pending))
if pending:
self._loop.create_task(self._wait_for_stop(pending))
async def _wait_for_stop(self, tasks):
await asyncio.gather(*tasks)
self._loop.stop() # << Stopping the event loop here.
_log.info("Loop stop initiated.")
...
还要提到的一件事是,文档说signal.signal()
处理程序not allowed
要与循环交互,但没有说明原因(请参阅)
推荐阅读
- tensorflow - 稀疏和分类类的 Keras ImageDataGenerator 流类索引
- reactjs - 从反应将数据传递给graphql的问题
- django - 使用 Django 登录后重定向
- python - RuntimeError:在应用程序上下文之外工作。与 app.app_context() 没有解决问题
- facebook - 为什么 Facebook 登录模式不提示用户访问他们的电子邮件地址?
- ios - 向空数组添加元素会引发错误
- javascript - 在不打开相机的情况下请求相机许可
- sql - sql查询通过选择不同列中2分钟日期之间的范围来计算每个id的行数
- angular - 使用 Angular 在 Ionic 5 中打开 ion-menu 时隐藏任何 div 或文本
- azure-iot-edge - IoT Edge 测试证书过期