Handling Infinite Loops on Server Applications

Discussion in 'Python' started by Paul Kozik, May 3, 2007.

  1. Paul Kozik

    Paul Kozik Guest

    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?
     
    Paul Kozik, May 3, 2007
    #1
    1. Advertising

  2. Paul Kozik

    MRAB Guest

    On May 3, 1:38 am, "Paul Kozik" <> 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)
     
    MRAB, May 3, 2007
    #2
    1. Advertising

  3. En Wed, 02 May 2007 21:38:52 -0300, Paul Kozik <> 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
     
    Gabriel Genellina, May 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joerg Lensing

    Debugging: Detecting infinite loops

    Joerg Lensing, Dec 14, 2003, in forum: Java
    Replies:
    2
    Views:
    666
  2. Mike Schilling
    Replies:
    11
    Views:
    1,463
    Mike Schilling
    Jun 12, 2004
  3. Andrej Hocevar

    infinite loops -- waiting for some event

    Andrej Hocevar, Jul 15, 2003, in forum: C Programming
    Replies:
    7
    Views:
    591
    Mark McIntyre
    Jul 25, 2003
  4. Rob

    help with infinite loops and scanf

    Rob, Jul 26, 2003, in forum: C Programming
    Replies:
    8
    Views:
    789
    Peter Shaggy Haywood
    Jul 29, 2003
  5. Vector

    infinite loops

    Vector, Mar 26, 2006, in forum: C++
    Replies:
    13
    Views:
    557
    Old Wolf
    Mar 27, 2006
Loading...

Share This Page