2016-04-28 83 views
0

我的限制是我最大的敵人,但最好的學習夥伴。從另一個類調用一個類的功能

我有兩個類:

class WorkerThread(Thread): 
"""Worker Thread Class.""" 
def __init__(self, notify_window): 
    """Init Worker Thread Class.""" 
    Thread.__init__(self) 
    self._notify_window = notify_window 
    self.ToKill = False 
    self._want_abort = 0 
    self.start() 

def run(self): 
    """Run Worker Thread.""" 
    while worker==True: 
     # somehow get to volumePanel.startStop() 

     if self.ToKill == True: 
      return None 
    proc.wait() 
    wx.PostEvent(self._notify_window, ResultEvent(None)) 
    return 

和:

class VolumePanel(wx.Panel): 
    #<...snip...> 
    def launchVolClick(self, event): 
     if self.bt_Launch.Label == "Enable Monitor": 
      self.worker = WorkerThread(self) 
      self.bt_Launch.Label = "Disable Monitor" 
     else: 
      self.worker.ToKill = True 
      self.bt_Launch.Label = "Enable Monitor" 

    def startStop(self): 
     print "in def startStop()" 

我想找到一個方法來調用startStopWorkerThread內。我試過this,無法讓它工作。

編輯:下面

class WorkerThread(Thread): 
    """Worker Thread Class.""" 
    def __init__(self, notify_window, func): 
     """Init Worker Thread Class.""" 
     Thread.__init__(self) 
     self._notify_window = notify_window 
     self.ToKill = False 
     self._want_abort = 0 
     global function 
     function = func 
     self.start() 

    def run(self): 
     """Run Worker Thread.""" 
     while worker==True: 
      # somehow get to volumePanel.startStop() 
      function() 
      if self.ToKill == True: 
       return None 
     proc.wait() 
     wx.PostEvent(self._notify_window, ResultEvent(None)) 
     return 

    def launchVolClick(self, event): 
     if self.bt_Launch.Label == "Enable Volume Monitor": 
      self.worker = WorkerThread(self, self.startStop) 
      self.bt_Launch.Label = "Disable Volume Monitor" 
     else: 
      self.worker.ToKill = True 
      self.bt_Launch.Label = "Enable Volume Monitor" 

回答

1

最後工作的代碼你可以傳遞一個參考startStop並從線程類中調用的引用作爲一個選項。沒有看到更多的代碼/你的代碼的結構如何,很難說其他選項。

這是前者的一個人爲的例子。你不必通過這種方式傳遞信息,你可以在VolumePanel中調用線程並通過self.startStop

另外,worker是undefined,所以是proc除非這個,如果wxpython,我不熟悉的一部分。

from threading import Thread 

class WorkerThread(Thread): 

    def __init__(self, func): 

     Thread.__init__(self) 
     self.func = func 

    def run(self): 

     for i in range(10): 
      self.func() 

class VolumePanel: 

    def __init__(self): 

     #self.thread = WorkerThread(self.startStop) 
     #self.thread.start() #or elsewhere 
     pass 

    def startStop(self): 

     print "in def startStop()" 

vp = VolumePanel() 
thread = WorkerThread(vp.startStop) 
thread.start() 
+0

我可以通過'高清startStop'爲'的WorkerThread(線程)'?我添加了上面啓動'WorkerThread'的事件,以查看清晰度是否有幫助。 – chow

+0

你引發了我需要知道的東西 - 我在'__init__'聲明中添加了'func',並添加了一個全局變量並且工作正常。 :)更新,上面的工作代碼。 – chow

+0

如果您將其設爲屬性,則不需要全局屬性。 'self.function'和'self.function()'。在'run'結尾處'return'也是不必要的。你可以合併你的兩個條件來避免'return None'。 '而工人,而不是self.ToKill' – Pythonista

0

您也可以passs調用類的線程像這樣:

class WorkerThread(threading.Thread): 
    def __init__(self, parent): 
     super(WorkerThread, self).__init__() 
     self.parent = parent 

    def run(self): 
     for i in range(10): 
      wx.CallAfter(self.parent.startStop) 


class VolumePanel(object): 
    def startStop(self): 
     print "in def startStop()" 

注意,我們使用wx.CallAfter()而不只是直接調用該函數。這是因爲如果我們直接調用它,它實際上是從線程調用而不是MainThread。這有時會成爲一個問題,取決於你在做什麼。

如果我們打印出當前線程(與threading.current_thread())無wx.CallAfter,我們得到

<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 
<WorkerThread(Thread-1, started 6612)> 

然而,隨着wx.CallAfter,我們得到

<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)> 
<_MainThread(MainThread, started 6604)>