[Twisted-Python] Cleanly shutting down Service

Alec Matusis matusis at matusis.com
Fri Apr 7 20:57:05 EDT 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  
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
server_connector = ConnectorFactory()
server_connector.protocol = protocols.IconsToSellersProtocol
server_connector.service = myservice
serv_to_serv = twisted.application.internet.TCPClient(HOST, PORT_1,

#this accepts connections from clients, I need this to close this during
client_factory = twisted.internet.protocol.ServerFactory()
client_factory.protocol = protocols.ClientProtocol
client_factory.service = myservice
client_factory.service.sc_protocol = server_connector.protocol

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
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

More information about the Twisted-Python mailing list