[Twisted-Python] unlistenTCP not working?

Jp Calderone exarkun at intarweb.us
Thu Jul 24 13:55:57 EDT 2003

On Thu, Jul 24, 2003 at 06:15:28PM +0100, Michael Porter wrote:
> Hi,
> I'm having a problem with unlistenTCP - it doesn't seem to work if
> application.listenTCP was called after the reactor is started. Am I missing
> something here or is this a bug? See test code below for demonstration...
> def listen(application, port):
>     from twisted.web import server, resource
>     print "called listen %d" % port
>     application.listenTCP(port, server.Site(resource.Resource()))
> def unlisten(application, port):
>     print "called unlisten %d" % port
>     application.unlistenTCP(port)
> def test(listenFirst):
>     from twisted.python import log
>     import sys
>     log.startLogging(sys.stdout, 0)
>     from twisted.internet import app, reactor
>     application = app.Application("test")
>     if listenFirst:
>         listen(application, 8080)
>     else:
>         reactor.callLater(0, listen, application, 8080)
>     reactor.callLater(5, unlisten, application, 8080)
>     application.run(save=0)
> If you call test(listenFirst = 1) then all works as expected (log shows it
> starts listening immediately and stops after 5 secs). But if you start
> listening by scheduling a call after application.run (call test(listenFirst
> = 0)) the listenTCP works but when unlisten is called 5 secs later it
> doesn't seem to stop listening and nothing appears in the log.
> (I'm running on Windows with twisted 1.0.6)
> Any ideas? Thanks,

  I believe this is somewhat intentional.  When the reactor is running,
listenXYZ() return Port objects which have a `stopListening' method.  It is
expected that listeners added while the application is running will have
this port object stashed away by the user code and, when the time comes,
have their `stopListening' method called.

  In practice, this can be somewhat difficult to manage, and is a good
reason to use ApplicationServices to manage listeners and connectors.

  I'm not sure if this could be "fixed", since it has behaved this way for a
long time, and some applications may depend on the behavior.


"Minerals are inexhaustible and will never be depleted. A stream of
investment creates additions to proved reserves, a very large in-ground
inventory, constantly renewed as it is extracted... How much was in the
ground at the start and how much will be left at the end are unknown and
irrelevant."  -- Morry Adelman, World Renowned Economist
-------------- 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/20030724/bf8f9788/attachment.pgp 

More information about the Twisted-Python mailing list