扭曲:捕获键盘中断并正确关闭
更新:为了便于阅读,以下是如何在反应器关闭之前添加回调:
reactor.addSystemEventTrigger('before', 'shutdown', callable)
原始问题如下。
如果我有一个客户端连接到服务器,并且它在反应器主循环中等待事件,当我按下 CTRL-C 时,我会收到“与另一端的连接以非干净的方式丢失:连接丢失。 ”我如何设置它以便我知道键盘中断何时发生,以便我可以进行适当的清理并干净地断开连接?或者如果可能的话,如何实现一种不涉及 CTRL-C 的更干净的关闭方式?
UPDATE: For ease of reading, here is how to add a callback before the reactor gets shutdown:
reactor.addSystemEventTrigger('before', 'shutdown', callable)
Original question follows.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您真的非常想专门捕获 Cc,那么您可以按照 Python 应用程序的常规方式执行此操作 - 使用
signal.signal
为SIGINT
安装一个处理程序做任何你想做的事。如果您从处理程序调用任何 Twisted API,请确保使用reactor.callFromThread
,因为几乎所有其他 Twisted API 对于从信号处理程序调用都是不安全的。但是,如果您真的只是对插入一些关闭时清理代码感兴趣,那么您可能需要使用 IService.stopService (或其实现机制,reactor .addSystemEventTrigger 来代替。
如果您使用的是
twistd
,那么使用IService.stopService
就很简单。您已经有一个Application
对象,并且至少附加了一项服务。您可以使用自定义stopService
方法添加另一个来完成关闭工作。该方法允许返回一个Deferred
。如果是,则关闭过程将暂停,直到Deferred
触发。这可以让您很好地清理连接,即使这涉及更多网络(或任何其他异步)操作。如果您不使用
twistd
,那么直接使用reactor.addSystemEventTrigger
可能会更容易。您可以安装一个关闭前触发器,该触发器将在调用IService.stopService
的相同情况下被调用。此触发器(任何可调用对象)还可以返回一个Deferred
来延迟关闭。这是通过调用reactor.addSystemEventTrigger('before', 'shutdown', callable)
来完成的(在启动关闭之前的某个时间,以便在关闭发生时它已经注册)。service.tac 提供了创建和使用自定义服务的示例。
wxacceptance.py 给出了使用
addSystemEventTrigger
的示例并将关机延迟(任意)三秒。每当反应堆停止时,这两种机制都会向您发出通知。这可能是由于 Cc 按键造成的,也可能是因为有人使用了
kill -INT ...
,或者可能是因为在某个地方调用了reactor.stop()
。它们都会导致反应堆关闭,而反应堆关闭总是会处理关闭事件触发器。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 forSIGINT
that does whatever you want to do. If you invoke any Twisted APIs from the handler, make sure you usereactor.callFromThread
since almost all other Twisted APIs are unsafe for invocation from signal handlers.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.If you're using
twistd
, then usingIService.stopService
is easy. You already have anApplication
object with at least one service attached to it. You can add another one with a customstopService
method that does your shutdown work. The method is allowed to return aDeferred
. If it does, then the shutdown process is paused until thatDeferred
fires. This lets you clean up your connections nicely, even if that involves some more network (or any other asynchronous) operations.If you're not using
twistd
, then usingreactor.addSystemEventTrigger
directly is probably easier. You can install a before shutdown trigger which will get called in the same circumstanceIService.stopService
would have been called. This trigger (just any callable object) can also return aDeferred
to delay shutdown. This is done with a call toreactor.addSystemEventTrigger('before', 'shutdown', callable)
(sometime before shutdown is initiated, so that it's already registered whenever shutdown does happen).service.tac gives an example of creating and using a custom service.
wxacceptance.py gives an example of using
addSystemEventTrigger
and delaying shutdown by (an arbitrary) three seconds.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 somewherereactor.stop()
was called. They all lead to reactor shutdown, and reactor shutdown always processes shutdown event triggers.我不确定你是在谈论你编写的客户端还是服务器。
无论如何,“CTRL-C”没有任何问题。
如果您将服务器编写为应用程序。来自
twisted.application.service.Service
的子类并定义startService
和stopService
。维护活动协议实例的列表。使用stopService
遍历它们并正常关闭它们。如果您有客户端,也可以对
Service
进行子类化,但使用reactor.addSystemEventTrigger('before','shutdown',myCleanUpFunction)
可能会更简单,并在此函数中优雅地关闭连接。I'm not sure whether you talking about a client or a server that you've written.
Anyway, nothing wrong with 'CTRL-C'.
If you're writing a server as an Application. Subclass from
twisted.application.service.Service
and definestartService
andstopService
. Maintain a list of active protocol instances. UsestopService
to go through them and close them gracefully.If you've got a client, you could also subclass
Service
, but it could be simpler to usereactor.addSystemEventTrigger('before','shutdown',myCleanUpFunction)
, and close connection(s) gracefully in this function.