[Twisted-Python] an easy twisted application question

Jp Calderone exarkun at intarweb.us
Tue Nov 11 12:50:35 MST 2003


On Tue, Nov 11, 2003 at 02:03:39PM -0500, Phil Christensen wrote:
> okay, so if i've got a tap with the following updateApplication method:
> 
> def updateApplication(app, config):
> 	app.registry = registry.Registry()
> 	...
> 	...
> 
> and i want to access that app.registry attribute from an arbitrary
> location, how do i do it?
> 
> i seem to remember hearing that there was only one instance of Application
> in a process, so does that mean that there's some (forgive the Java-speak)
> static method defined on the Application module that will get the current
> running instance?

  The old way of doing this would be...

    from twisted.internet.app import theApplication
    theApplication.registry

  However, this bites.

  Newly written programs should implement makeService() instead of
updateApplication().  As a bonus, purely service-based programs can easily
and cleanly do things like the above:

    from twisted.application import service

    class RegistryService(service.Service):
        def __init__(self):
            self.registry = registry.Registry()

    def makeService(config):
        s = RegistryService()
        ...
        anotherS = SomeotherService()
        anotherS.setServiceParent(s)  # Or s.setServiceParent(anotherS)
        ...
        return s # or return anotherS

  `s' can be the parent service of other services, in which case it can be
accessed simply by looking up the parent service, or it could be a child
service of something else, in which case it would be accessed via a call to
getServiceNamed() on the appropriate parent.

  This avoids the need for globals and lets services/applications be more
readily configurable alongside other services/applications (since neither
will have global state that might interfer with the other).

  Jp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: </pipermail/twisted-python/attachments/20031111/c0128514/attachment.sig>


More information about the Twisted-Python mailing list