扭曲:正确捕获键盘中断并关闭 [英] twisted: catch keyboardinterrupt and shutdown properly

查看:34
本文介绍了扭曲:正确捕获键盘中断并关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:为了便于阅读,这里是如何在反应器关闭之前添加回调:

UPDATE: For ease of reading, here is how to add a callback before the reactor gets shutdown:

reactor.addSystemEventTrigger('before', 'shutdown', callable)

原始问题如下.

如果我有一个客户端连接到服务器,并且它在反应堆主循环中等待事件,当我按下 CTRL-C 时,我得到一个与另一端的连接以非干净的方式丢失:连接丢失."如何设置它以便我知道何时发生键盘中断,以便我可以进行适当的清理并干净地断开连接?或者,如果可能的话,我如何实现一种不涉及 CTRL-C 的更简洁的关闭方式?

If I have a client connected to a server, and it's chilling in the reactor main loop waiting for events, when I hit CTRL-C, I get a "Connection to the other side was lost in a non-clean fashion: Connection lost." How can I set it up so that I know when a KeyboardInterrupt happens, so that I can do proper clean-up and disconnect cleanly? Or how can I implement a cleaner way to shutdown that doesn't involve CTRL-C, if possible?

推荐答案

如果你真的、真的想专门捕获 Cc,那么你可以用 Python 应用程序的通常方式来做到这一点 - 使用 signal.signalSIGINT 安装一个可以做任何你想做的事情的处理程序.如果您从处理程序调用任何 Twisted API,请确保使用 reactor.callFromThread,因为几乎所有其他 Twisted API 对于从信号处理程序调用都是不安全的.

If you really, really want to catch C-c specifically, then you can do this in the usual way for a Python application - use signal.signal to install a handler for SIGINT that does whatever you want to do. If you invoke any Twisted APIs from the handler, make sure you use reactor.callFromThread since almost all other Twisted APIs are unsafe for invocation from signal handlers.

但是,如果您真的只是对插入一些关闭时清理代码感兴趣,那么您可能想要使用 IService.stopService(或实现它的机制,reactor.addSystemEventTrigger) 代替.

However, if you're really just interested in inserting some shutdown-time cleanup code, then you probably want to use IService.stopService (or the mechanism in terms of which it is implemented,reactor.addSystemEventTrigger) instead.

如果您使用 twitd,那么使用 IService.stopService 很容易.您已经有一个 Application 对象,其中至少附加了一项服务.您可以使用自定义 stopService 方法添加另一个用于关闭工作的方法.该方法可以返回一个 Deferred.如果是,则关闭过程将暂停,直到 Deferred 触发.这让您可以很好地清理连接,即使这涉及更多网络(或任何其他异步)操作.

If you're using twistd, then using IService.stopService is easy. You already have an Application object with at least one service attached to it. You can add another one with a custom stopService method that does your shutdown work. The method is allowed to return a Deferred. If it does, then the shutdown process is paused until that Deferred fires. This lets you clean up your connections nicely, even if that involves some more network (or any other asynchronous) operations.

如果您不使用 twistd,那么直接使用 reactor.addSystemEventTrigger 可能更容易.您可以安装一个 before shutdown 触发器,它会在 IService.stopService 被调用的相同情况下被调用.这个触发器(只是任何可调用的对象)也可以返回一个 Deferred 来延迟关闭.这是通过调用 reactor.addSystemEventTrigger('before', 'shutdown', callable) 完成的(在启动关闭之前的某个时间,以便在关闭时它已经注册).

If you're not using twistd, then using reactor.addSystemEventTrigger directly is probably easier. You can install a before shutdown trigger which will get called in the same circumstance IService.stopService would have been called. This trigger (just any callable object) can also return a Deferred to delay shutdown. This is done with a call to reactor.addSystemEventTrigger('before', 'shutdown', callable) (sometime before shutdown is initiated, so that it's already registered whenever shutdown does happen).

service.tac 给出了创建和使用自定义的示例服务.

service.tac gives an example of creating and using a custom service.

wxacceptance.py 给出了一个使用 addSystemEventTrigger 的例子 并将关机延迟(任意)三秒.

wxacceptance.py gives an example of using addSystemEventTrigger and delaying shutdown by (an arbitrary) three seconds.

这两种机制都会在每当反应堆停止时给你通知.这可能是因为 Cc 按键,或者可能是因为有人使用了 kill -INT ...,或者可能是因为在某个地方调用了 reactor.stop().它们都会导致反应堆关闭,反应堆关闭总是处理关闭事件触发器.

Both of these mechanisms will give you notification whenever the reactor is stopping. This may be due to a C-c keystroke, or it may be because someone used kill -INT ..., or it may be because somewhere reactor.stop() was called. They all lead to reactor shutdown, and reactor shutdown always processes shutdown event triggers.

这篇关于扭曲:正确捕获键盘中断并关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆