[Twisted-Python] Error handling in Application.bindPorts
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
app = twisted.internet.app.Application("daytimer")
f = Factory()
f.protocol = Daytime
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 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.
 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/
More information about the Twisted-Python