[Twisted-Python] Getting Rid of Application in a Compatible Way

Moshe Zadka m at moshez.org
Fri May 23 03:02:47 EDT 2003


I suggest to write ApplicationService classes which look like:

class TCPListener(ApplicationService):

    def __init__(self, port, factory, backlog=5, interface='', *args, **kwargs):
        self.port = port
        self.factory = factory
        self.backlog = backlog
        self.interface = interface
        ApplicationService.__init__(self, *args, **kwargs)

    def __getstate__(self):
        d = self.__dict__.copy()
        d['_port'] = None
        return d

    def startService(self):
        ApplicationService.startService(self)
        self._port = reactor.listenTCP(self.port, self.factory, self.backlog,
                                       self.interface)

    def stopService(Self):
        ApplicationService.stopService(self)
        self._port.stopListening()
        self._port = None

Now, instead of application.listenTCP we can use

TCPListener(8080, server.Site(resource), "foo").setServiceParent(application)

The only problem is needing to choose unique names. I suggest
ApplicationService will lose the unPythonic
        if not isinstance(serviceName, types.StringType):
            raise TypeError("%s is not a string." % serviceName)
And add to app.py

class Unique:
    pass

This means that "unnamed" services are now possible:
TCPListener(8080, server.Site(resource), Unique()).setServiceParent(application)

You can still access a service by its "serviceName", but you have to get
the name from the service. This means stuff like cred will still work.
Of course, similar Services can be written for UNIXListener, SSLListener,
etc.

-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/




More information about the Twisted-Python mailing list