Handling Infinite Loops on Server Applications

P

Paul Kozik

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?
 
M

MRAB

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)
 
G

Gabriel Genellina

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.
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top