[Twisted-Python] Error handling in Application.bindPorts

Bernhard Herzog bh at intevation.de
Thu Jan 23 14:23:56 EST 2003


I'm trying to get one of my twisted programs to terminate immediately if
it can't listen on a port on startup. It's main program is basically the
same as in e.g. doc/howto/listings/application/app1.py, i.e (quoting
from app1.py):

app = twisted.internet.app.Application("daytimer")
f = Factory()
f.protocol = Daytime
app.listenTCP(8813, f)


My program uses a different protocol and factory, but the principle is
the same: it calls app.listenTCP before it calls app.run().

Now, if the mainloop isn't running yet, listenTCP doesn't start
listening immediately[1] but simply appends the arguments to a list.
Then, when app.run() is called and that list is not empty it will call
bindPorts before entering the mainloop.

bindPorts simply catches the CannotListenError, logs a message and
returns immediately even if not all ports have been bound yet.
Unfortunately, even if bindPorts returns early, app.run still enters the
mainloop. There doesn't seem to be a good way to avoid this.

IMO bindPorts should reraise the exception after writing logging the
error instead of simply returning. 


[1] I think app.listenTCP doesn't try to listen immediately because this
method is also used when creating taps and that should be possible even
if the port is currently in use or the user doesn't have permission to
listen on that port.

Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/

More information about the Twisted-Python mailing list