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

Terry Jones terry at jon.es
Mon Nov 3 21:33:59 EST 2008


Hi again.

Despite all the useful suggestions, I've just realized that I still can't
complete what I was originally trying to do...

I'm trying to make a Twisted service. In my twisted/plugins/myService.py
file I have something like this:

    from zope.interface import implements
    from twisted.plugin import IPlugin
    from twisted.application.service import IServiceMaker
    from twisted.python import usage
    from my.application import myservice

    class Options(usage.Options):
        optParameters = [ ['db-uri', None, None, 'A db URI'], ]

    class MyServiceMaker(object):
        implements(IServiceMaker, IPlugin)
        tapname = 'My service'
        description = 'Desc.'
        options = Options

        def makeService(self, options):
            return myservice.MyService(dbURI=options['db-uri'])

    myServiceMaker = MyServiceMaker()


The problem here is that the service class I want to instantiate and return
from makeService calls something that returns a Deferred.

In Glyph's classmethod, Esteve's __new__, and my metaclass code, the thing
you get back when you go to use class is a Deferred. You have to wait for
that Deferred to fire before you get your instance. In JP's approach you
also wait for a Deferred before instantiating your class.

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

If not, wouldn't it be nice if makeService allowed you to return a Deferred?
AKA teaching your grandmother to suck eggs :-)

Terry




More information about the Twisted-Python mailing list