Issue with wxPython GUI

Laszlo Nagy gandalf at shopzeus.com
Tue Nov 13 07:18:12 EST 2007


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









More information about the Python-list mailing list