473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SocketServer and timers

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
Jul 28 '06 #1
5 2005

alf wrote:
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.
--
--Bryan

Jul 28 '06 #2
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

Jul 28 '06 #3

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

Jul 28 '06 #4
br***********************@yahoo.com wrote:
....
>
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
Ach! You're right. I didn't consider that handle_request() might
block..

Jul 29 '06 #5
alf
br***********************@yahoo.com wrote:
alf wrote:
>>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.

thx for a suggestion, will try it out ...
Aug 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Olivier Hoarau | last post by:
Hello, I have build a client/server application with the socket module. The server mades UDP broadcasting and the client only reads UDP broadcast messages. All work fine. Now I want to use for...
3
by: Ergin Aytac | last post by:
I'm trying to run a script written in python and have some socket connection problems. I cutted the origin script (more than 1000 lines) so it is only the part of the connection and there is no...
0
by: Adil Hasan | last post by:
Hello Fred, I just ran across your question. I think that the following code will work: ----- SERVER CODE ------ import SocketServer import time class...
5
by: missiplicity | last post by:
Hi, I am newbie to Python language and am taking my baby steps. I am using Python2.4 from ActiveState on W2K. I am trying to create a simple SocketServer program. Just adding the following 2 lines...
12
by: Paul Rubin | last post by:
Let's say you have a SocketServer with the threading mix-in and you run serve_forever on it. How can you shut it down, or rather, how can it even shut itself down? Even if you use a...
3
by: Magnus Lycka | last post by:
I have a socket server like below which I want to exit when it's out of data. If I interrupt the client, I'll get a broken pipe on the server side, and after a Ctrl-C, I can restart the server...
1
by: rbt | last post by:
I've read more about sockets and now, I have a better understanding of them. However, I still have a few SocketServer module questions: When used with SocketServer how exactly does...
0
by: Tomi Hautakoski | last post by:
Hello, I'm a Python newbie trying to figure out how to use SocketServer with IPv6. I would like to set up a TCPServer working like below but how to tell SocketServer I need to use AF_INET6? ...
5
by: eliben | last post by:
Hello, I have a small wxPython application. Today I was trying to add some RPC capability to it, so I implemented an instance of SimpleXMLRPCServer that runs in a separate thread when invoked...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.