2016-09-13 2 views
0

최소의 작동 예제는 황금 표준이며 현재 작업 중입니다. 그러나 명백한 오류가있을 수 있습니다. 함수 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

'ph_control'이 함수를 반환합니까? Process 객체를 생성하기도 전에 호출하기 때문에 바인딩 된 메소드처럼 'args'에'bot' 인수가 필요 없을 것입니다. pHBot이 다른 프로세스에서 작동 할 때 기대하는 바를 수행하는지 여부는 또 다른 질문입니다. 이런 종류의 동작으로 이어지는 또 다른 공통적 인 문제는 단지 출력을 플러시하지 않는 것입니다 ('sys.stdout.flush()'). 그러나 나는 그것이 이번이라고 생각하지 않습니다. –

답변

0
+0

당신은 대신에 deamon으로 프로그램을 시작하고 다중 처리를 사용하지 말아야합니까? – Moritz

+0

@Moritz 당신의 bot.ph_control()은 데몬으로 실행되어 메인 스레드를 멈추지 않아야합니다. – Juggernaut

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 
관련 문제