twisted: catch keyboardinterrupt and shutdown properly

后端 未结 2 2147
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 07:33

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

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


        
2条回答
  •  萌比男神i
    2021-02-07 08:32

    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 define startService and stopService. Maintain a list of active protocol instances. Use stopService 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 use reactor.addSystemEventTrigger('before','shutdown',myCleanUpFunction), and close connection(s) gracefully in this function.

提交回复
热议问题