Gabriel Genellina wrote:
En Fri, 15 May 2009 09:04:05 -0300, Igor Katson escribió:
Lawrence D'Oliveiro wrote:
In message <mailman.185.1242375959.8015.python-l...@python.org>, Igor Katson wrote:
Lawrence D'Oliveiro wrote:
In message <mailman.183.1242371089.8015.python-l...@python.org>, Igor Katson wrote:

I have problems in getting a SocketServer to shutdown.

Do you want to do a shutdown or a close?

I want the server close the socket ...


You want to do a close, do a close, not a shutdown <http://docs.python.org/library/socket.html>.

Shutdown implies closing the listening socket, doesn't it?

No (perhaps it should, but that is another issue). There is a
documentation bug; BaseServer.shutdown is documented as "Tells the
serve_forever() loop to stop and waits until it does." [1]
The docstring is much more explicit: """Stops the serve_forever loop.
Blocks until the loop has finished. This must be called while
serve_forever() is running in another thread, or it will deadlock."""

So, if you have a single-threaded server, *don't* use shutdown(). And, to
orderly close the listening socket, use server_close() instead. Your code
would become:

   from SocketServer import TCPServer, BaseRequestHandler

server = TCPServer(('localhost',1234), BaseRequestHandler)
try:
      server.serve_forever()
except KeyboardInterrupt:
      print "^C detected"
      pass
finally:
      print "server_close()"
      server.server_close()
print "bye"

(I've opened http://bugs.python.org/issue6031 )

[1]
http://docs.python.org/dev/library/socketserver.html?highlight=baseserver#SocketServer.BaseServer.shutdown

Hmm. Gabriel, could you please show the same for the threaded version? This one deadlocks:

from SocketServer import TCPServer, BaseRequestHandler
from threading import Thread

server = TCPServer(('localhost',1234), BaseRequestHandler)
try:
   run_thread = Thread(target=server.serve_forever)
   run_thread.start()
   run_thread.join()
except KeyboardInterrupt:
   print "^C detected"
   pass
finally:
   print "server_shutdown()"
   kill_thread = Thread(target=server.shutdown)
   kill_thread.start()
print "bye"
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to