executing python script from wxpython GUI (button click)..

David Bolen db3l at fitlinxx.com
Fri Jun 11 10:32:52 EDT 2004


sarmin kho <sarmin_kho at yahoo.com> writes:

> i ve tryed the command 'execfile(name of the python script)'.. this
> command will stall the GUI until the python script exited.

If the script won't be cooperating with the GUI (e.g., isn't built to
be run in an Idle event routine or to do its work in chunks), then
your best bet is to execfile the script in a separate thread.  You can
create a worker thread class that your GUI instantiates (or you can
keep one or more around that reads from a queue for work to do), and
let that thread handle the execution.  If you need to communicate back
to the GUI thread (say for stdout from the script or for other
reasons), you'll need to generate events through wxPython back to the
main thread, since only the main thread can perform most GUI
manipulations (wxPostEvent being an exception).

The information at
  
    http://wiki.wxpython.org/index.cgi/LongRunningTasks

while not entirely applicable might help (e.g., if you use the
threading approach and replace the long computation with the
execfile).

Beyond that it's probably better to post to the wxPython mailing list
where you may have a better target audience.

Oh, and note that doing it this way does imply that if the script you
are executing is one that would interfere with Python threading itself
(e.g., perhaps it calls out to an extension module that doesn't
release the GIL around a blocking or CPU-bound operation), then this
mechanism won't help, since even having the script in its own thread
will still block Python in general.  In such a case you're only
recourse would be to execute the script in a second process and handle
some form of communication between the main GUI process and the script
executing process.

And lastly, I should note (although hopefully you're already aware of
it) that using execfile as a scripting mechanism can be a significant
exposure to your script if you expect to be using "foreign" scripts.
Even if you lock down the globals/locals in the environment that the
script you are execing runs in, it can import arbitrary modules and
perform arbitrary actions within the context of your main script.  As
long as that's reasonable for the target environment, that's fine, but
it's something to be aware of.

-- David



More information about the Python-list mailing list