我知道,最小的工作示例是黄金标准,我正在研究它。但是,也许有一个明显的错误。函数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)
我建议你在这里学习守护进程: https://pypi.python.org/pypi/python-daemon/
答案 1 :(得分: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