<div><font color="#990000">Hello All,</font></div>
<div><font color="#990000"></font> </div>
<div><font color="#990000">I got the following example from </font><a href="http://wiki.wxpython.org/LongRunningTasks"><font color="#990000"><strong>http://wiki.wxpython.org/LongRunningTasks</strong></font></a></div>
<div><font color="#990000">This example shows how a worker thread can be created from the main GUI and the main GUI is notified when the worker thread has done its work..</font></div>
<div>
<div><font color="#990000"> </font></div>
<div><font color="#990000">But my requirement is:</font></div>
<div><font color="#990000">I want to have two worker threads and each one notifies the main thread. It could be possible that when a worker thread is running, other might be tirggerd. </font></div>
<div><font color="#990000">1) Can I do with only one Worker thread class?</font></div>
<div><font color="#990000">2) Can there be only one notification function for multiple threads.</font></div>
<div> </div>
<div>
<div><strong>CODE:</strong></div>
<div><strong></strong> </div>
<div><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial">import time<br>from threading import *<br>import wx</span></div>
<div>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"># Button definitions<br>ID_START = wx.NewId()<br>ID_STOP = wx.NewId()</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"># Define notification event for thread completion<br>EVT_RESULT_ID = wx.NewId()</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial">def EVT_RESULT(win, func):<br> """Define Result Event."""<br> win.Connect(-1, -1, EVT_RESULT_ID, func)
</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial">class ResultEvent(wx.PyEvent):<br> """Simple event to carry arbitrary result data."""<br> def __init__(self, data):
<br> """Init Result Event."""<br> wx.PyEvent.__init__(self)<br> self.SetEventType(EVT_RESULT_ID)<br> self.data = data</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"># Thread class that executes processing<br>class WorkerThread(Thread):<br> """Worker Thread Class."""
<br> def __init__(self, notify_window):<br> """Init Worker Thread Class."""<br> Thread.__init__(self)<br> self._notify_window = notify_window<br> self._want_abort = 0
<br> # This starts the thread running on creation, but you could<br> # also make the GUI thread responsible for calling this<br> self.start()</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> def run(self):<br> """Run Worker Thread."""<br> # This is the code executing in the new thread. Simulation of
<br> # a long process (well, 10s here) as a simple loop - you will<br> # need to structure your processing so that you periodically<br> # peek at the abort variable<br> for i in range(10):<br>
time.sleep(1)<br> if self._want_abort:<br> # Use a result of None to acknowledge the abort (of<br> # course you can use whatever you'd like or even<br> # a separate event type)
<br> wx.PostEvent(self._notify_window, ResultEvent(None))<br> return<br> # Here's where the result would be returned (this is an<br> # example fixed result of the number 10, but it could be
<br> # any Python object)<br> wx.PostEvent(self._notify_window, ResultEvent(10))</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> def abort(self):<br> """abort worker thread."""<br> # Method for use by main thread to signal an abort
<br> self._want_abort = 1</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"># GUI Frame class that spins off the worker thread<br>class MainFrame(wx.Frame):<br> """Class MainFrame."""
<br> def __init__(self, parent, id):<br> """Create the MainFrame."""<br> wx.Frame.__init__(self, parent, id, 'Thread Test')</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> # Dumb sample frame with two buttons<br> wx.Button(self, ID_START, 'Start', pos=(0,0))<br> wx.Button
(self, ID_STOP, 'Stop', pos=(0,50))<br> self.status = wx.StaticText(self, -1, '', pos=(0,100))</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> self.Bind(wx.EVT_BUTTON, self.OnStart, id=ID_START)<br> self.Bind(wx.EVT_BUTTON, self.OnStop, id=ID_STOP)</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> # Set up event handler for any worker thread results<br> EVT_RESULT(self,self.OnResult)</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> # And indicate we don't have a worker thread yet<br> self.worker = None</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> def OnStart(self, event):<br> """Start Computation."""<br> # Trigger the worker thread unless it's already busy
<br> if not self.worker:<br> self.status.SetLabel('Starting computation')<br> self.worker = WorkerThread(self)</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> def OnStop(self, event):<br> """Stop Computation."""<br> # Flag the worker thread to stop if running
<br> if self.worker:<br> self.status.SetLabel('Trying to abort computation')<br> self.worker.abort()</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial"> def OnResult(self, event):<br> """Show Result status."""<br> if event.data is None:
<br> # Thread aborted (using our convention of None return)<br> self.status.SetLabel('Computation aborted')<br> else:<br> # Process results here<br> self.status.SetLabel
('Computation Result: %s' % event.data)<br> # In either event, the worker is done<br> self.worker = None</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial">class MainApp(wx.App):<br> """Class Main App."""<br> def OnInit(self):<br> """Init Main App."""
<br> self.frame = MainFrame(None, -1)<br> self.frame.Show(True)<br> self.SetTopWindow(self.frame)<br> return True</span></p>
<p style="BACKGROUND: white"><span style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: Arial">if __name__ == '__main__':<br> app = MainApp(0)<br> app.MainLoop()</span></p>
<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="FONT-SIZE: 8pt; COLOR: blue"><font face="Times New Roman"> </font></span><font color="#660000">Help would be appreciated.</font></p></div></div>
<div><font color="#660000"> </font></div>
<div><font color="#660000">Thanks in advance</font></div>
<div><font color="#660000"> </font></div>
<div><font color="#660000">Regards,</font></div>
<div><font color="#660000">Tarun</font></div>
<div><font color="#660000"></font> </div></div>
<div> </div>