SocketServer and timers

A

alf

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
 
B

bryanjugglercryptographer

alf said:
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

Override serve_forever() and replace "while 1:" with something like
"while self.continue_flag:".
-how to implement timers

You could override get_request(), and use select.select() to wait for
either the socket to become readable (so the accept() won't block), or
a timeout to expire. If you are not already, use the threading or
forking
mixin so that the request handler won't stop everthing if it blocks.
 
S

Simon Forman

alf said:
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
 
B

bryanjugglercryptographer

Simon said:
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().
 
S

Simon Forman

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().

Ach! You're right. I didn't consider that handle_request() might
block..
 
A

alf

Override serve_forever() and replace "while 1:" with something like
"while self.continue_flag:".




You could override get_request(), and use select.select() to wait for
either the socket to become readable (so the accept() won't block), or
a timeout to expire. If you are not already, use the threading or
forking
mixin so that the request handler won't stop everthing if it blocks.

thx for a suggestion, will try it out ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top