closing a "forever" Server Socket

alessandro dilbert01 at gmail.com
Mon Jan 22 12:49:02 EST 2007


Oh my God! it's really so complicated?

3 modules (threading, SocketServer, select) only for design a way to
shutdown a TCP server????
...but they told me that python was easy... :)

I'm working on a simulator and I have a monitor server that collects
information. I can shutdown it using Ctrl-C from the keyboard but for
my purpose could be very nice if I introduce a timer. So I could launch
my monitor like this:

./monitor 100

and my monitor will run for 100 seconds. For this I'm using the Timer
class provided by threading module, I have implemented a function like
this:

def shutdown():
    sys.exit()

but it doesen't work because the server remain alive...maybe
SocketServer create immortal server...
I need only to close my application, there is a way to force the server
thread to close?

thanks!
Alessandro



Laszlo Nagy ha scritto:

> alessandro írta:
> > thanks
> >
> > infact the server_forever() method is only a serve() method inside an
> > infinite loop.
> >
> > many thanks again,
> >
> Here is a snipped that show a "software terminateable threading TCP
> socker server". The "server" object is a SocketServer instance,
> server_stopped is a threading.Event instance. You should also import the
> "select" module.
>
>         srvfd = server.fileno()
>         while not server_stopped.isSet():
>             ready = select.select([srvfd], [], [], 1) # Give one second
> for incoming connection so we can stop the server in seconds
>             if srvfd in ready[0]:
>                 server.handle_request()
>             else:
>                 pass  # log('No incoming connection, retrying')




More information about the Python-list mailing list