简体   繁体   中英

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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM