Issue with wxPython GUI

kyosohma at gmail.com kyosohma at gmail.com
Tue Nov 13 09:01:26 EST 2007


On Nov 13, 6:18 am, Laszlo Nagy <gand... at shopzeus.com> wrote:
> tarun írta:> Hi Laszlo Nagy,
>
> > Thanks a lot.
> > But the issue over here is that how will the child thread acknowledge
> > the main thread that it has completed its task. For this I'll have to
> > set some flag in the child thread and poll for it in the main thread.
> > This will create a problem.
>
> What kind of problem, can you explain? Supposing your child (worker)
> thread created the output, I would do it this way (untested):
>
> class Child(threading.Thread):
>     def __init__(self,output):
>        self.stop_requested = threading.Event()
>        self.stopped = threading.Event()
>        self.output = output
>        ....
>
>     def run(self):
>        try:
>            while not self.stop_requested.isSet():
>                 # put your algorithm here
>                 ....
>                 # but check periodically if you need to abort the job.
>                 if self.stop_requested.isSet():
>                    break
>                 ...
>                 # when you have data, put into the output queue
>                 self.output.put( your_output )
>        finally:
>           self.stopped.set()
>
> -- And then, from the main thread, create the worker:
>
> self.queue = Queue()
> self.child = Child(self.queue)
>
> -- wxApp.OnIdle:
>
> if not self.queue.empty():
>     data = self.queue.get()
>     # display your data here, for example add to a text control.
>
> if self.child.stopped.isSet():
>     add_log("Job compete") # tell the user what is going on
>
> And finally, you can have a button for stopping the script:
>
> def OnClick(self,event):
>     self.child.stop_requested.set()
>     add_log("stopping worker thread....")   # tell the user what is going on
>
> > Any alternatives??
>
> First you should tell us what is the problem with using two threads.
>
> Regards,
>
>    Laszlo

I run separate tasks in a separate thread. See http://wiki.wxpython.org/LongRunningTasks
for more details.

You should also look at the wxPython demo as it has some examples of
using threads in it. One other way to post messages to the main thread
is by using pubsub. This link describes it:

http://wiki.wxpython.org/ModelViewController

as does this: http://www.wxpython.org/docs/api/wx.lib.pubsub-module.html

HTH

Mike




More information about the Python-list mailing list