daphne本身也需要在服务器关闭时,做些事情(主动断掉所有connections)
参考了源码,找到了解决方案。代码如下:
from twisted.internet import reactor
def before_shutdown():
print('Succeed!')
reactor.addSystemEventTrigger("before", "shutdown", before_shutdown)
async def application(scope, receive, send):
pass

这篇博客介绍了如何利用Twisted的`reactor.addSystemEventTrigger`方法在服务器关闭前执行清理任务,例如断开所有连接。示例代码展示了在关闭前调用`before_shutdown`函数来确保资源的正确释放。
756

被折叠的 条评论
为什么被折叠?



