我正在尝试编写一个SIGTERM处理程序,该处理程序将具有我的 run_forever()-循环
这是我写的学习样本:
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.
可悲的是,“ Thud。”行表示 thumper(..)演示电话实际上已经 结论,不会显示。我想这是因为“聚会”给我带来了一套 未实现的期货。但是,如果我敢激活 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
问题归结为
程序应在 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术语,这也就不足为奇了。 / em>
答案 0 :(得分:1)
Python信号处理程序在运行循环的同一线程中的main thread中执行。 self._shutdown
方法不会立即停止循环,而只是sets a flag,因此,当您的循环下次运行时,它仅执行已经安排的回调,并且不再安排任何回调(请参见run_forever)。但是,直到信号处理程序返回之前,循环才能运行。这意味着您不能等到循环在信号处理程序中停止为止。相反,您可以安排另一个任务,该任务将等待长时间运行的任务对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
处理程序是{{1}}与循环交互,而没有说明原因(see)
答案 1 :(得分:0)
找到了一个解决方案,该解决方案将从异步函数中调用 self._loop.stop() 首先要等待所有其他任务。请注意,它不会自行等待! 如果尝试,该程序将锁定。
此外, asyncio.wait_for(..)协同程序允许超时。
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)
async def _a_exit(self):
self._shutdown = True
my_task = asyncio.current_task()
pending = list(filter(lambda x: x is not my_task, asyncio.all_tasks(loop=self._loop)))
waiters = [asyncio.wait_for(p, timeout = 1.5, loop=self._loop) for p in pending]
results = await asyncio.gather(*waiters, loop=self._loop, return_exceptions=True)
n_failure = len(list(filter(lambda x: isinstance(x, Exception), results)))
_log.info(f"{n_failure} failed processes when quick-gathering the remaining {len(results)} tasks. Stopping loop now.")
self._loop.stop()
def _exit(self, sig, frame):
name = signal.Signals(sig).name
_log.info(f"Received shutdown-signal: {sig} ({name})")
self._loop.create_task(self._a_exit())
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', 1))
loop.create_task(lp.thumper('South Hall', 2))
loop.create_task(lp.thumper(' West Hall', 3))
loop.create_task(lp.thumper(' East Hall', 4))
loop.run_forever()
_log.info("Done.")
在Windows 10上,这可能会导致输出
North Hall: Winding up...
South Hall: Winding up...
West Hall: Winding up...
East Hall: Winding up...
North Hall: Thump!
South Hall: Thump!
[..]
South Hall: Thump!
North Hall: Thump!
14:20:59 INFO [__main__]: Received shutdown-signal: 2 (SIGINT)
West Hall: Thump!
West Hall: Thud.
North Hall: Thump!
North Hall: Thud.
South Hall: Thump!
South Hall: Thud.
14:21:01 INFO [__main__]: 1 failed processes when quick-gathering the remaining 4 tasks. Stopping loop now.
14:21:01 INFO [__main__]: Done.
失败的进程成为超时的牺牲品。
请注意,这解决了我的问题。但是,关于 loop.run_until_complete(..)为什么在调用 loop.stop()后失败的问题仍然悬而未决。