繁体   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