[Twisted-Python] an easy twisted application question

Moshe Zadka m at zadka.site.co.il
Tue Nov 11 23:39:40 MST 2003


On Tue, 11 Nov 2003, Jp Calderone <exarkun at intarweb.us> wrote:

>     from twisted.application import service
> 
>     class RegistryService(service.Service):
>         def __init__(self):
>             self.registry =3D registry.Registry()
> 
>     def makeService(config):
>         s =3D RegistryService()
>         ...
>         anotherS =3D SomeotherService()
>         anotherS.setServiceParent(s)  # Or s.setServiceParent(anotherS)
>         ...
>         return s # or return anotherS

Almost correct.
The idiomatic way is

class SomeOtherService
    # .... do something interesting here
    def registry(self):
        return self.parent.getServiceNamed('registry')

def makeService(config):
    s = RegistryService()
    s.setName('registry')
    anotherS = SomeOtherService()
    m = service.MultiService()
    s.setServiceParent(m)
    anotherS.setServiceParent(m)
    return m

The trick is to have 's' and 'anotherS' completely symmetrical,
and allow them to access each other.

Everything else Jp said, including the part about "globals bad,
explicit references good" has my whole-hearted blessing.




More information about the Twisted-Python mailing list