[Twisted-Python] One big smile ...

Moshe Zadka m at moshez.org
Sat Jul 12 17:05:05 MDT 2003


On 12 Jul 2003, Abe Fettig <abe at fettig.net> wrote:

> class SimpleService(app.ApplicationService):
>     def startService(self):
>         self.serviceRunning = 1

Call the father's method first, instead:
app.ApplicationService.startService(self)
...
Ditto for stopService(self).
They are responsible for setting .serviceRunning

>         reactor.callLater(0, self.printHello)

Why not self.nextCall = reactor.callLater(0, ...)?

>     def stopService(self):
>         self.serviceRunning = 0
>         if hasattr(self, 'nextCall'): 
>             self.nextCall.cancel()
>             del(self.nextCall)
>         print "%s: Stopped." % self.serviceName

You also want to avoid persisting nextCall

def __getstate__(self):
    d = self.__dict__
    if 'nextCall' in d:
        del d['nextCall']
    return d

> class ServiceWebManager(resource.Resource):
>     isLeaf = 1
> 
>     def __init__(self, app):
>         self.app = app
> 
>     def render(self, request):
>         serviceName = request.args.get('service', [''])[0]
>         action = request.args.get('action', [''])[0]
>         if serviceName:
>             service = self.app.getServiceNamed(serviceName)
>             if action == 'Start':
>                 service.startService()
>             else:
>                 service.stopService()
>             request.redirect('/')

Note that your service has a bug if .startService() is called
twice without an intervening stopService(), and you present
here for users an ability to tickle that bug. You can probably
work around in many ways...

...but I prefer to having non-parents .enable() or .disable()
the service. Of course, it can start enabled by default, but
this way you have a clear distinction which methods are supposed
to be called by whom.
 
> myApp = app.Application("Test App")
> SimpleService("Service 1", myApp)
> SimpleService("Service 2", myApp)
> website = server.Site(ServiceWebManager(myApp))
> myApp.listenTCP(8008, website)

If you call it "application" insteadof "myApp", the next line
is not needed: instead of "Python"ing it, "twistd -y" it
[or maybe twistd -ny if you don't want forking]

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