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

S

sarmin kho

Hi Pythoners,

i m working on wxpython GUI from where a button clicked will run a python script. while the python script is running, the GUI (wxpython) will remain responsive to user. the GUI will display some results from the running python script (run by a button clicked on the GUI).

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

any helps, please..

regards
sarmin
 
D

David Bolen

sarmin kho said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top