[Twisted-Python] startService not called in finger14.py ?
Martin Waite
martin at datacash.com
Thu Dec 11 06:25:09 EST 2003
Hi,
It seems that FingerService.stopService() and
FingerService.startService() never actually get called
in finger14.py in the tutorial.
I have added logging into startService() and stopService()
to trace when they get called:
==========================================================
# Read from file
from twisted.application import internet, service
from twisted.internet import protocol, reactor, defer
from twisted.protocols import basic
from twisted.python import log
class FingerProtocol(basic.LineReceiver):
def lineReceived(self, user):
self.factory.getUser(user
).addErrback(lambda _: "Internal error in server"
).addCallback(lambda m:
(self.transport.write(m+"\r\n"),
self.transport.loseConnection()))
class FingerService(service.Service):
def __init__(self, filename):
self.users = {}
self.filename = filename
def startService(self):
def _read(self):
for line in file(self.file):
user, status = line.split(':', 1)
user=user.strip()
status=status.strip()
self.users[user] = status
self.call = reactor.callLater(30, _read)
_read()
log.msg( "startService" )
service.Service.startService(self)
def stopService(self):
log.msg( "stopService" )
service.Service.stopService(self)
self.call.cancel()
def getUser(self, user):
return defer.succeed(self.users.get(user, "No such user"))
def getFingerFactory(self):
log.msg( "getFingerFactory" )
f = protocol.ServerFactory()
f.protocol, f.getUser = FingerProtocol, self.getUser,
f.startService = self.startService
return f
application = service.Application('finger', uid=1, gid=1)
f = FingerService('/home/martin/users')
finger = internet.TCPServer(1079, f.getFingerFactory())
finger.setServiceParent(service.IServiceCollection(application))
==========================================================
Running the example:
twistd2.2 -noy finger14.py
2003/12/11 11:20 GMT [-] Log opened.
2003/12/11 11:20 GMT [-] twistd 1.1.1 (/usr/bin/python2.2 2.2.1) starting up
2003/12/11 11:20 GMT [-] reactor class: twisted.internet.default.SelectReactor
2003/12/11 11:20 GMT [-] Loading finger14.py...
2003/12/11 11:20 GMT [-] getFingerFactory
2003/12/11 11:20 GMT [-] Loaded.
2003/12/11 11:20 GMT [-] twisted.internet.protocol.ServerFactory starting on 1079
2003/12/11 11:20 GMT [-] Starting factory <twisted.internet.protocol.ServerFactory instance at 0x828e5e4>
<ctrl-C>
2003/12/11 11:20 GMT [-] (Port 1079 Closed)
2003/12/11 11:20 GMT [-] Stopping factory <twisted.internet.protocol.ServerFactory instance at 0x828e5e4>
2003/12/11 11:20 GMT [-] Main loop terminated.
2003/12/11 11:20 GMT [-] Server Shut Down.
==========================
This is bad news for me because I'm trying to write a service
based on this code. I really want stopService() to be called
so I can cleanly close down my app, but I can't figure out
how to do it.
Can anyone help please ?
regards,
Martin
More information about the Twisted-Python
mailing list