简体   繁体   English

多重处理:主程序停止,直到过程完成

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

I know, a minimal working example is the gold standard and I am working on it. 我知道,一个最低限度的工作示例是黄金标准,我正在为此努力。 However, maybe there is an obvious error. 但是,也许有一个明显的错误。 The function run_worker is executed upon a button press event. 在按下按钮事件时执行功能run_worker It initiates a class instance and should start a method of that class. 它启动一个类实例,并应启动该类的方法。 However the function run_worker waits until the class method has finished. 但是,函数run_worker会等到类方法完成之后。 As a result kivy gets stuck and I cannot do other stuff. 结果,猕猴桃卡住了,我无法做其他事情。 Any ideas how I should use multiprocessing in this case ? 有什么想法我应该在这种情况下使用多处理吗?

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

I found a solution. 我找到了解决方案。 Not sure why it works: 不知道为什么它起作用:

    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.

相关问题 Flask 多处理在进程完成之前不会返回响应 - Flask multiprocessing does not return the response until a process is finished 主进程完成后,Python 多处理需要很长时间才能完成 - Python multiprocessing takes too long to complete after main process has finished 在python中,使用多处理池时,主进程在某些情况下无法完成 - In python, when use multiprocessing Pool, main process can not finished in some situation 与主程序的multiprocessing.Manager()通信不起作用 - multiprocessing.Manager() communication with main programm didn't work 将 Multiprocessing 与 Python Flask 2.7 一起使用会挂起主应用程序,直到进程完成 - Using Multiprocessing with Python Flask 2.7 hangs the main app until process completes Python多重处理-将新功能分配给完成的过程吗? - Python multiprocessing - Allocate a new function to a finished process? Python 多处理:主进程的名称 - Python multiprocessing: name of the main process Python 多处理:子进程完成但没有加入 - Python Multiprocessing: The child process finished but did not join 从 main 终止多处理进程 - Terminating a multiprocessing process from main 主线程在子线程完成之前不会执行 - main thread not execute until child thread finished
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM