SocketServer and timers

Simon Forman rogue_pedro at yahoo.com
Fri Jul 28 03:30:08 EDT 2006


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

HTH,
~Simon




More information about the Python-list mailing list