wxpython thread question

Mike Driscoll kyosohma at gmail.com
Mon Feb 11 09:50:23 EST 2008


On Feb 10, 10:26 pm, marcin... at gmail.com wrote:
> I am new to threading and python. Currently I am working on a GUI app
> that will be a frontend to CLI client.
>
> I want to invoke a method of the class from GUI and run it in the
> thread. That works well, the problems is I dont know how can I stop
> this operation, thread if the user would like to stop/cancel it.
>
> Here is the code:
>
> .
> .
> .
>
> class ScanThread:
>     def __init__(self, txtbox, para):
>         self.para = para
>         self.txtbox = txtbox
>         #print "Init"
>
>     def Start(self):
>         self.keepGoing = self.running = True
>         thread.start_new_thread(self.Run, ())
>         #print "Start"
>
>     def Stop(self):
>         self.keepGoing = False
>         #print "Stop"
>
>     def IsRunning(self):
>         return self.running
>         #print "IsRunning"
>
>     def Run(self):
>         if self.keepGoing:
>                 obj=tool.sanner(self.para,self.txtbox);
>
>                 obj.scan()
>                 obj.attack()
>                 #time.sleep(1)
>
>         self.running = False
>         #print "Run set to false"
>
> .
> .
> .
>
>  def do_Scan(self, event): # wxGlade: MyFrame.<event_handler>
>         #print "Event handler `do_Scan' not implemented"
>         self.threads = []
>         val = self.text_ctrl_1.GetValue()
>         box = self.text_ctrl_2;
>         self.threads.append(ScanThread(box,val))
>         for t in self.threads:
>             t.Start()
>         self.Scan.Enable(False)
>         self.Stop.Enable(True)
>         self.frame_1_statusbar.SetStatusText("Scanning")
>         #event.Skip()
>
>  def do_Stop(self, event): # wxGlade: MyFrame.<event_handler>
>         #print "Event handler `do_Stop' not implemented"
>
>         busy = wx.BusyInfo("One moment please, waiting for threads to
> die...")
>         wx.Yield()
>
>         for t in self.threads:
>             t.Stop()
>
>         running = 1
>
>         while running:
>             running = 0
>
>             for t in self.threads:
>                 running = running + t.IsRunning()
>
>             time.sleep(0.1)
>         #self.Destroy()
>         #event.Skip()
>
> do_Stop doesn't stop the Thread. Works well if I put the simple code
> in the thread (i.e printing text etc), but with CLI class code I am
> using, it simply doesn't respond.
>
> Any help and explanation or code sample GREATLY appreciated.
>
> Do I have to rewrite class I am using (CLI client) to work with
> threads to make this happen ????
>
> Thanks,

This is a confusing post as Dennis pointed out. I am glad he was so
analytical. Anyway, if you had posted to the wxPython list, they
probably would have told you to check out the Demo and the wiki. The
Demo has a threading example called DelayedResult, which shows one way
to "stop" a thread. However, I think Dennis had an excellent
explanation of thread killing as well. The wiki link is below:

http://wiki.wxpython.org/LongRunningTasks

Mike



More information about the Python-list mailing list