Andriy Basilisk wrote:
Hello,
I have made a simple wxPython app that recursively processes a number of files in a specified directory tree at the click of a "GO" button. It works very well without problems. The question now is: how can I allow the user to pause or stop the process? The "file processor" is a simple function in another module. It may run for a long while and may need to be paused or stopped altogether. I can add a "Pause" and a "Stop" button to the main frame, but how would I make it interrupt the running function? Is it possible?
If you only need to abort and never continue, the easiest thing is to make the callback function return a boolean. The worker function using the callback can read the boolean value and if it is False, abort.
If you need to let the worker continue after pausing, you tend to do the same refactoring the function to split its job in succesive calls. This means, if your worker is basically a loop, make it so that instead of looping, calling the function once will go once through the loop and exit. In order to know what work was done and where to continue, you usually create a state structure which the function uses as input/output. This is basically what C functions like opendir()/readdir()/closedir() do for directory traversal.
Having said this... we just reimplemented generators! Go take a look at Python PEP 342 for this very functionality implemented in the language, which allows you to write prettier code. Its available in the "What's new" section of the Python 2.5 documentation.
And don't forget to check wxPython demo DelayedResult, which shows how to make the GUI responsive putting your worker function in a separate thread.
···
--
Rastertech España S.A.
Grzegorz Adam Hankiewicz
/Jefe de Producto TeraVial/
C/ Perfumería 21. Nave I. Polígono industrial La Mina
28770 Colmenar Viejo. Madrid (España)
Tel. +34 918 467 390 (Ext.17) *·* Fax +34 918 457 889
ghankiewicz@rastertech.es *·* www.rastertech.es <http://www.rastertech.es/>