By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Handling Infinite Loops on Server Applications

P: n/a
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?
May 3 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 3, 1:38 am, "Paul Kozik" <zyk...@gmail.comwrote:
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)

May 3 '07 #2

P: n/a
En Wed, 02 May 2007 21:38:52 -0300, Paul Kozik <zy****@gmail.comescribió:
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
May 3 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.