[Twisted-Python] Twistd and application framework questions

Itamar Shtull-Trauring itamar at itamarst.org
Thu Apr 2 06:40:14 MDT 2009


On Wed, 2009-04-01 at 13:16 -0400, Nadav Aharony wrote:

> 1) The program uses multiple UDP/TCP clients and servers that are 
> currently launched with reactor.listenTCP / UDP connectTCP/UDP etc. I've 
> been using MultiService, according to the twisted documentation.
> Some of these services were are at a top level of my app so it was easy 
> to turn that part into a .tac file and switch the reacor calls to 
> internet.TCPServer etc.
> However, some of them are deep inside my code, and are instantiated on 
> the fly.
> 
> What is the "right" way to attach them to my service parent? (the part 
> with ".setServiceParent(<myMultiservice>")
> Should I now add a pointer to my MultiService that will be propagated 
> down the  code hierarchy to each of these calls and be accessible at the 
> inner scopes?
> Is there a neater way to do it just with importing the right modules? 
> (in the same way that in usual twisted scripts the loaded reactor is a 
> global reactor)
> 
> 2)  There are still scenarios where I would want to run my code the 
> "reactor" way rather than than using the application framework, and I 
> would love to be able to keep a single file that's compatible with both 
> modes..
> Is there a way to detect in runtime whether the code is being run 
> through 'twistd' and the app framework or if its run directly?
> I was thinking of doing something like:
> 
> if <test if we are run as an app>:
>     internet.TCPServer(<port>,<factory>).setServiceParent(<myMultiservice>)
> else:

1. You don't need to attach to parents service, you can also call
start/stopService directly.

2. You can still call "self.port = reactor.listenTCP(...)" directly in a
custom startService, and self.port.stopListening() in a custom
stopService. You don't have to use twisted.application.internet, it
doesn't add much beyond a little convenience.

The point of services is to encapsulate startup and shutdown logic, not
to make your life harder :)





More information about the Twisted-Python mailing list