Simon Forman wrote: > alf wrote: > > Hi, > > > > I have one thread app using SocketServer and use server_forever() as a > > main loop. All works fine, but now I need certain timer checking let's > > say every 1 second something and stopping the main loop. So questions are: > > -how to stop serve_forever > > -how to implement timers > > > > thx, alf > > Do you really need a timer, or do you just want to check something > every second or so? > > If the latter, then something like this would do it: > > from time import time > > INTERVAL = 1.0 > > RUN = True > > while RUN: > > # Get a time in the future. > T = time() + INTERVAL > > # Serve requests until then. > while time() < T: > server.handle_request() > # Check whatever. > if check(): > # Do something, for example, stop running. > RUN = False
That alone does not work. If server.handle_request() blocks, you don't get to the check(). You need some kind of timeout in handle_request(). -- --Bryan -- http://mail.python.org/mailman/listinfo/python-list