[Twisted-Python] Need Exception that will stop ther reactor within twistd

Nicolas D. Cesar ncesar at lunix.com.ar
Fri Nov 24 12:14:37 EST 2006

I have the following code, 

from twisted.application.internet import TCPServer
from twisted.application import service
from twisted.internet import protocol
from twisted.python.failure import DefaultException

class GoodByeException(DefaultException):
    def __init__(self, *args, **kwargs):
        DefaultException.__init__(self, *args, **kwargs)
        self.args = "I wanna quit....\n but I'm stuck inside a twisted 

class FortyTwoProtocol(protocol.Protocol):
    def connectionMade(self):
	raise GoodByeException()

class FortyTwoFactory(protocol.ServerFactory):
    protocol = FortyTwoProtocol
    def __init__(self):

application = service.Application("Total Perspective Vortex")
theService  = service.MultiService()
fact42      = FortyTwoFactory()
server42    = TCPServer(4242, fact42)

I want my GoodByeException to exit the reactor loop (basacally do a 
reactor.stop()) and then twistd exit the reactor's main loop. I'm programming 
a bigger application and sudentlly some exceptions are really Fatal, but the 
applications keeps on running (sometimes with no services attending).

Any sugestions will be appreciated .


PS: this is the log
$ twistd -noy miniserver.tac
2006/11/24 14:05 ART [-] Log opened.
2006/11/24 14:05 ART [-] twistd 2.4.0 (/usr/bin/python 2.4.4) starting up
2006/11/24 14:05 ART [-] reactor class: 
<class 'twisted.internet.selectreactor.SelectReactor'>
2006/11/24 14:05 ART [-] Loading miniserver.tac...
2006/11/24 14:05 ART [-] Loaded.
2006/11/24 14:05 ART [-] __builtin__.FortyTwoFactory starting on 4242
2006/11/24 14:05 ART [-] Starting factory <__builtin__.FortyTwoFactory 
instance at 0xa7850a2c>
2006/11/24 14:05 ART [__builtin__.FortyTwoFactory] Traceback (most recent call 
          File "/usr/lib/python2.4/site-packages/twisted/python/log.py", line 
38, in callWithContext
            return context.call({ILogContext: newCtx}, func, *args, **kw)
          File "/usr/lib/python2.4/site-packages/twisted/python/context.py", 
line 59, in callWithContext
            return self.currentContext().callWithContext(ctx, func, *args, 
          File "/usr/lib/python2.4/site-packages/twisted/python/context.py", 
line 37, in callWithContext
            return func(*args,**kw)
File "/usr/lib/python2.4/site-packages/twisted/internet/selectreactor.py", 
line 139, in _doReadOrWrite
            why = getattr(selectable, method)()
        --- <exception caught here> ---
          File "/usr/lib/python2.4/site-packages/twisted/internet/tcp.py", 
line 769, in doRead
File "/usr/lib/python2.4/site-packages/twisted/internet/protocol.py", line 
333, in makeConnection
          File "miniserver.tac", line 14, in connectionMade
            raise GoodByeException()
        __builtin__.GoodByeException: I wanna quit....
         but I'm stuck inside a twisted application

2006/11/24 14:05 ART [-] Received SIGINT, shutting down.
2006/11/24 14:05 ART [-] (Port 4242 Closed)
2006/11/24 14:05 ART [-] Stopping factory <__builtin__.FortyTwoFactory 
instance at 0xa7850a2c>
2006/11/24 14:05 ART [-] Main loop terminated.
2006/11/24 14:05 ART [-] Server Shut Down.

Nicolás D. César <ncesar at lunix.com.ar>
Lunix S.R.L. -[ http://www.lunix.com.ar ]-
GnuPG Public Key: gpg --keyserver wwwkeys.pgp.net --recv-key 0x3606F3E6
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20061124/021684f9/attachment.pgp 

More information about the Twisted-Python mailing list