Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Handling Infinite Loops on Server Applications

Reply
Thread Tools

Handling Infinite Loops on Server Applications

 
 
Paul Kozik
Guest
Posts: n/a
 
      05-03-2007
I'm working with a small server program I'm writing for a small video
game. The main class constructor starts a thread that handles socket
connections, which itself starts new threads for each user connection.

The actual server program itself however needs to wait in the
background, but continue looping as not to close the running threads.
The problem is, simply running a [while True: pass] main loop in this
style eats precious CPU cycles (and for nothing). If it waits for
input, such as a socket.accept() or raw_input(), this problem does not
occur (obviously because it's not constantly looping).

What would be the best way to handle this, perhaps in a fashion
similar to how most server programs are handled (with arguments such
as [apache start], [apache stop])? Any guides towards this type of
application development?
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      05-03-2007
On May 3, 1:38 am, "Paul Kozik" <(E-Mail Removed)> wrote:
> I'm working with a small server program I'm writing for a small video
> game. The main class constructor starts a thread that handles socket
> connections, which itself starts new threads for each user connection.
>
> The actual server program itself however needs to wait in the
> background, but continue looping as not to close the running threads.
> The problem is, simply running a [while True: pass] main loop in this
> style eats precious CPU cycles (and for nothing). If it waits for
> input, such as a socket.accept() or raw_input(), this problem does not
> occur (obviously because it's not constantly looping).
>
> What would be the best way to handle this, perhaps in a fashion
> similar to how most server programs are handled (with arguments such
> as [apache start], [apache stop])? Any guides towards this type of
> application development?
>

You could put a sleep in the loop:

import time
while True:
# Sleep for 1 minute, or whatever...
time.sleep(60)

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-03-2007
En Wed, 02 May 2007 21:38:52 -0300, Paul Kozik <(E-Mail Removed)> escribió:

> I'm working with a small server program I'm writing for a small video
> game. The main class constructor starts a thread that handles socket
> connections, which itself starts new threads for each user connection.


And what's the purpose of the main thread then?

> The actual server program itself however needs to wait in the
> background, but continue looping as not to close the running threads.
> The problem is, simply running a [while True: pass] main loop in this
> style eats precious CPU cycles (and for nothing). If it waits for
> input, such as a socket.accept() or raw_input(), this problem does not
> occur (obviously because it's not constantly looping).


Exactly. Use the network handling thread as the main thread, by example.

> What would be the best way to handle this, perhaps in a fashion
> similar to how most server programs are handled (with arguments such
> as [apache start], [apache stop])? Any guides towards this type of
> application development?


See the asyncore module, or any of the predefined servers in the Python
library.

--
Gabriel Genellina
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
infinite loops Vector C++ 13 03-27-2006 09:37 PM
Infinite loops in hashCode() and equals() Mike Schilling Java 11 06-12-2004 04:46 AM
Debugging: Detecting infinite loops Joerg Lensing Java 2 12-15-2003 12:03 PM
help with infinite loops and scanf Rob C Programming 8 07-29-2003 12:28 AM
infinite loops -- waiting for some event Andrej Hocevar C Programming 7 07-24-2003 11:05 PM



Advertisments