473,383 Members | 1,759 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,383 software developers and data experts.

Handling Infinite Loops on Server Applications

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
2 1381
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
2
by: psg | last post by:
Hello! I am developing Windows application which after sending a request to a server will listen to response, a response will be infinite in length, but will not be continuous in time. What I...
2
by: William F. Robertson, Jr. | last post by:
Some of my users are receiving an error: Server Error in '/' Application. ---------------------------------------------------------------------------- ---- Request timed out. Description: An...
33
by: Anthony England | last post by:
I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on...
59
by: rami | last post by:
please everybody ,can anyone tell me how to do an infinite loop in C
10
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
2
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString",...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.