#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    635
    Rep Power
    66

    Smile Pausing a while loop


    I have an endless while loop that I would like to pause by pressing the p key and then start up up again by pressing the same key later. I am really stumped.

    In case it matters, I am using Windows XP.
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,810
    Rep Power
    1571
    Originally Posted by Dietrich
    I have an endless while loop that I would like to pause by pressing the p key and then start up up again by pressing the same key later. I am really stumped.

    In case it matters, I am using Windows XP.
    First off, is this a console app, or a GUI app? I assume the latter, given that you mentioned Windows. If so, what windowing library are you using? The event model in question will matter.

    Second, can it be stopped at the beginning of the next cycle, or is it necessary to stop the loop at the exact point of the keyboard event? If the former, you can just change the while: conditional to check for a keyboard event, then if it is a 'p', have is loop and wait on the keyboard event until another 'p' is issued. Otherwise, you would have to set up two threads, one running the program, the second doing a tight wait on the keyboard, and suspending or restarting the first thread on a 'p'.

    Can you provide your code, or at least enough of it to show the specific problem?
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    635
    Rep Power
    66

    Smile


    It is really simple console code right now:
    Code:
    import time
    
    i = 0
    while True:
        print i
        i += 1
        time.sleep(1)
        # like to pause when key p is hit
        # end resume loop if key p is hit again
        
        # for testing, so it's not endless
        if i > 25:  break
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    This seems relatively easy to me.
    Code:
    import thread, time
    i = 0
    
    class key: p = 0
    
    def RunLoop():
        while 1:
            print i
            i += 1
            time.sleep(1)
            if p gets hit: key.p = 1; break
    
    while 1:
        if key.p is 1: pass
        else: RunLoop()
        if p gets hit and key.p is 1: key.p = 0
    I havn't tested this, but see how it works. Also there is some pseudo-code. 'gets hit'. You may need to modify it a bit, but I think you get the idea.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    11
    Originally Posted by Dietrich
    I have an endless while loop that I would like to pause by pressing the p key and then start up up again by pressing the same key later. I am really stumped.

    In case it matters, I am using Windows XP.
    How to determine when a key is pressed will depend on what you're using - you can't do it in vanilla Python, but you can if you use Tkinter, pygame, etc.

    To suspend in the middle of a while loop, use a generator:
    Code:
    def whilefunc():
        i = 0
        while True:
           i += 1
           print i
           if True:     # replace with call to keycheck function
                yield None
    wf = whilefunc().next
    
    wf()
    print "!"
    wf()
    --OH.

IMN logo majestic logo threadwatch logo seochat tools logo