[Twisted-Python] Cleanly shutting down Service

Alec Matusis matusis at matusis.com
Fri Apr 7 18:57:05 MDT 2006


I have two related questions about this.

I have been using

reactor.addSystemEventTrigger('before', 'shutdown',
myservice.beforeStopService) (not using .tac)

In my beforeStopService() function I need to do two things:

1) I have two factories, and two different protocols, that use two different
ports. Both have the same application set as service parent. I would like to
shut down connections on only ONE of the two ports before I proceed with
pre-shutdown procedures. How do I do this?

I am only aware of  
twisted.internet.reactor.disconnectAll()
But that shuts down all connections on BOTH ports.

Here is the simplified structure of my application:

#this connects to another server, I need this to stay open during
pre-shutdown
server_connector = ConnectorFactory()
server_connector.protocol = protocols.IconsToSellersProtocol
server_connector.service = myservice
serv_to_serv = twisted.application.internet.TCPClient(HOST, PORT_1,
server_connector).setServiceParent(application)

#this accepts connections from clients, I need this to close this during
pre-shutdown
client_factory = twisted.internet.protocol.ServerFactory()
client_factory.protocol = protocols.ClientProtocol
client_factory.service = myservice
client_factory.service.sc_protocol = server_connector.protocol
client_serv=twisted.application.internet.TCPServer(PORT_2,
client_factory).setServiceParent(application)

2) This scenario is simliar to 1), except that I want to stop accepting new
connections on PORT_2 in my pre-shutdown procedure, leaving the existent
connections on PORT_2 in ESTABLISHED state intact. 


-----Original Message-----
From: twisted-python-bounces at twistedmatrix.com
[mailto:twisted-python-bounces at twistedmatrix.com] On Behalf Of Itamar
Shtull-Trauring
Sent: Monday, April 03, 2006 8:36 AM
To: Twisted general discussion
Subject: Re: [Twisted-Python] Cleanly shutting down Service

On Sun, 2006-04-02 at 11:21 -0700, apocalypznow wrote:
> How do I cleanly shut down a Service?  I want the shut down to make the 
> stopService() method get called.

Either use twistd with a .tac, as someone else suggested, or:

reactor.addSystemEventTrigger('before', 'shutdown', myservice.stopService)

If stopService returns a Deferred this will delay shutdown until
Deferred returned from stopService is triggered.


_______________________________________________
Twisted-Python mailing list
Twisted-Python at twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list