Function stopping a function

Duncan Booth duncan.booth at invalid.invalid
Fri Nov 23 09:45:14 CET 2007


Sorin Schwimmer <sxn02 at yahoo.com> wrote:

> For instance, lenghty_function() executes, when an
> external event triggers cancel(), which is supposed to
> abruptly stop lengthy_function(), reset some variables
> and exit immediately.
> 

def lenghty_function(some, arguments, abort=lambda: False):
   while not abort():
      do some part of the function


then you just pass in whatever abort function is appropriate: it can check 
some flag to see whether the function should continue, or it might just 
check the current time and return True if the function has been going too 
long.
Just make sure that the badly spelled function calls abort() often.



More information about the Python-list mailing list