Designing a cancellable function
Gabriel Genellina
gagsl-py at yahoo.com.ar
Fri Dec 15 22:22:58 EST 2006
At Friday 15/12/2006 22:20, Leo Breebaart wrote:
>I have written a function foo() that iterates over and processes
>a large number of files. The function should be available to the
>user as library function, via a command-line interface, and
>through a GUI.
>
>So, I added a 'config' object as a parameter to foo() that can be
>used by the caller to explicitly pass in user-defined settings.
>Because the processing foo() does can take such a long time, the
>next thing I did was add an 'update_function' callback parameter
>that foo() will call regularly, so that the GUI can update a
>progress bar, and the command-line version can print dots, etc.
>
>I now would also like to add the possibility to allow the user to
>*cancel* the execution of foo() during the processing, and I am
>wondering what the best / most Pythonic way to design this is.
I can't say if this is the "best/more Pythonic way", but a simple way
would be to use the return value from your callback. Consider it an
"abort" function: if it returns True, cancel execution; as long as it
returns False, keep going.
(The somewhat "reversed" meaning is useful in case the user doesn't
provide a callback at all, or it's an empty one, or it contains just
a print ".", statement, all of these returning False; so, to actually
abort the process it must have an explicit "return True" statement).
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
More information about the Python-list
mailing list