簡體   English   中英

多重處理:主程序停止,直到過程完成

[英]Multiprocessing: main programm stops until process is finished

我知道,一個最低限度的工作示例是黃金標准,我正在為此努力。 但是,也許有一個明顯的錯誤。 在按下按鈕事件時執行功能run_worker 它啟動一個類實例,並應啟動該類的方法。 但是,函數run_worker會等到類方法完成之后。 結果,獼猴桃卡住了,我無法做其他事情。 有什么想法我應該在這種情況下使用多處理嗎?

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')

我找到了解決方案。 不知道為什么它起作用:

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM