[Twisted-Python] A Python metaclass for Twisted allowing __init__ to return a Deferred

glyph at divmod.com glyph at divmod.com
Tue Nov 4 06:11:27 EST 2008

On 02:33 am, terry at jon.es wrote:
>That would be fine if makeService could in turn return a Deferred that
>would result in an object implementing IServiceMaker, but as far as I 
>tell, makeService does not allow this. And in the context of a Twisted
>plugin file like the above I'm even less clear on how to go about it.
>Am I missing something here?

As a matter of fact, yes :).

A Service instance is effectively a piece of static configuration.  In 
your example, you need to do reactor work; communicate with a remote 
database in a thread.  Services very definitely should _not_ be 
generating network traffic, scheduling timed calls, or otherwise talking 
to the reactor until "startService" has been called.  The idea with a 
Service is that you should be able to write a serialization mechanism 
that loads it out of a text file, a string in memory, or in your case a 
set of command line arguments, without touching any other objects, so 
that it can be re-configured and re-serialized by configuration tools.

You may want to create some _other_ configuration object, loaded from 
the remote database, in the middle of startService.  You may even want 
to have a multi-phase internal startup procedure to generate that 
object.  But you need to keep track of what state those things are in so 
that the infrastructure can come around and say stopService to you in 
the middle of that procedure and still get a clean, ordered shutdown. 
If the infrastructure were to get back a Deferred, there's nothing to 
call stopService on.

More information about the Twisted-Python mailing list