2016-09-13 87 views
0

我知道,一個最小的工作示例是黃金標準,我正在處理它。但是,也許有一個明顯的錯誤。在按鈕按下事件時執行功能run_worker。它啓動一個類實例,並應該啓動該類的一個方法。然而函數run_worker會等到類方法結束。結果kivy卡住了,我無法做其他的事情。任何想法如何在這種情況下使用多處理?多處理:主程序停止,直到處理完成

from multiprocessing import Process 

class SettingsApp(App): 
    """ Short not working version of the actual programm 
    """ 
    def build(self): 
     """Some kivy specific settings""" 
     return Interface 

"""This part does not work as expected. It is run by pushing a button. However, The function does hang until the Process has finished (or has been killed).""" 

    def run_worker(self): 
     """The pHBot application is started as a second process. Otherwise kivy would be blocked until the function stops 
     (which is controlled by the close button) 
     """ 
     # get the arguments in appropriate form 
     args = self.get_stored_settings() 

     # Initiate the class which should be run by a separate process 
     bot = pHBot(*args) 

     # the control method runs some devices, listens to sensors etc. 
     phbot = Process(target=bot.ph_control(), args=(bot,)) 

     # start the process 
     phbot.start() 

     # This statement is executed only after phbot has stopped. 
     print('Started') 
+0

要看,確實我們需要一個工作的例子。 –

+0

'ph_control'是否返回一個函數?因爲在創建Process對象之前調用它(很可能你不需要'args'中的'bot'參數,因爲它是一個綁定方法)。另一個問題是pHBot在另一個過程中的運行是否符合您的期望。導致這種行爲的另一個常見問題是不刷新輸出('sys.stdout.flush()'),但我不認爲這次是這樣。 –

回答

0

我找到了解決辦法。不知道爲什麼它的工作原理:

def worker(self): 
     """ 
     The pHBot application is started as a second process. Otherwise kivy would be blocked until the function stops 
     (which is controlled by the close button) 
     """ 
     # initiate the process 
     args = self.get_stored_settings() 
     bot = pHBot(*args) 
     bot.ph_control() 

    def run_worker(self): 
     """The function is called upon button press 
     """ 
     # start the process, is 
     self.phbot.start() 
     pass # not sure if that is necessary