【问题标题】:How to implement callbacks for SIGTERM and SIGINT signals with aiohttp如何使用 aiohttp 实现 SIGTERM 和 SIGINT 信号的回调
【发布时间】:2019-06-27 23:07:13
【问题描述】:

我正在尝试对由 aiohttp 运行的 Web 服务器进行正常关闭。我需要它来优雅地关闭和清理 redis 和 DB 连接。

对于我在不同文档中看到的有关此问题的讨论,我已经使用主循环的 add_signal_handler 注册了一个回调,但当我的服务器关闭时仍然没有触发回调。

这是我得到的:

def gracefull_shutdown(signame, loop):
   logging.debug('graceful shutdown callback')

app = web.Application()

if __name__ == '__main__':
   asyncioLoop = asyncio.get_event_loop()
   for signame in {'SIGINT', 'SIGTERM'}:
       asyncioLoop.add_signal_handler(
           getattr(signal, signame),
           functools.partial(gracefull_shutdown, signame, asyncioLoop)
           )

   web.run_app(app, handle_signals=True)

你有什么想法吗?提前感谢您的回答

【问题讨论】:

    标签: signals python-3.7 aiohttp


    【解决方案1】:

    好的,官方文档中解释了解决方案,但不容易找到。您可以通过 Web 应用程序对象的 on_shutdown 属性注册回调。

    https://docs.aiohttp.org/en/stable/web_advanced.html#aiohttp-web-graceful-shutdown

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2020-05-11
      • 2016-11-09
      • 2017-08-22
      • 1970-01-01
      相关资源
      最近更新 更多