[Tutor] Stop a long loop in python console or in wxpython?

phpfood phpfood at gmail.com
Mon Jun 8 13:41:02 CEST 2009

Thanks for the reply Alan. I'm unfamiliar on both methods you suggested. Can
you give me examples with code of them?
I saw this piece of code on your webpage:

import msvcrt

def doKeyEvent(key):
    if key == '\x00' or key == '\xe0': # non ASCII
       key = msvcrt.getch() # fetch second character
    print ord(key),

def doQuitEvent(key):
    raise SystemExit

# First, clear the screen of clutter then warn the user
# of what to do to quit
lines = 25 # set to number of lines in console
for line in range(lines): print

print "Hit space to end..."

# Now mainloop runs "forever"
while True:
   ky = msvcrt.getch()
   length = len(ky)
   if length != 0:
      # send events to event handling functions
      if ky == " ": # check for quit event

Is this similar to the try except clause method?
How will breaking code into timed events help me? I wanted to stop the
function on an event (like pushing a button). The part I don't really
understand is how the program can be screening or "recognizing" a command or
event while it hasn't finished this other process. How does the timer help
in this? From my experiences with python, python doesn't let anything get in
its way until its done with the current process unless you end it
immediately with ctrl c or pressing X. It won't even let you type text into
the console while it's "in process."

On Mon, Jun 8, 2009 at 4:23 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> "xbmuncher" <xboxmuncher at gmail.com> wrote
>  def longLoop():
>>   x = 0
>>   for n in range(100000):
>>       time.sleep(5)
>>       x = x + n
>> It will run in the console for a long time. How can I send a command and
>> cause it to stop and pursue hello() function?
> You can use Ctrl-C and put the loop insiode a try/except clause
> to catch the Interrupt. Or....
>  in the console.. and really I'd like to create a GUI that will run the
>> longLoop() function after I push a button and then press another button to
>> stop the longLoop() function and run the hello() function.
> There are two normal ways to do this in a GUI:
> 1) break the loop into small chunks and run them in response
> to timer events. The function then calls the timer at the end of
> each chunk until it completes its task. This frees up the GUI to
> detect user events between timers.
> 2) Use threads. This has the advantage of working in a GUI
> or a commandline. But is a little bit more complex and you
> need to make sure your loop isn't messing with (or being
> messed with) the rest of the priogram via shared data/resources.
>  in the wxPython, which is gui of choice for python. I'll settle for a way
>> to
>> do it via console as well. The overall idea is, is that I want to cut
>> short
>> the process of a piece of code in order to start another piece of code.
> You can put a terminating flag in the timer code or you can
> send a message to stop a thread, either way will work.
> HTH,
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090608/16b35939/attachment.htm>

More information about the Tutor mailing list