2011-02-23 6 views
5

GUI 응용 프로그램 (wxPython)을 만들고 있습니다. GUI 응용 프로그램에서 다른 (.exe) 응용 프로그램을 실행해야합니다. 하위 프로세스는 사용자 작업에 대해 일부 작업을 수행하고 GUI 응용 프로그램에 출력을 반환합니다.다른 스레드의 루프에서 다른 프로세스를 실행하는 방법

이 하위 프로세스를 루프로 실행하므로 지속적으로 하위 프로세스를 실행할 수 있습니다. 내가하고있는 일은 스레드를 시작하고 (그래서 gui는 동결하지 않는다.) 서브 프로세스를 루프에 넣고 을 popen한다. 이것이 최선의 방법인지 확실하지 않습니다.

self.thread = threading.Thread(target=self.run, args=()) 
self.thread.setDaemon(True) 
self.thread.start() 

def run(self): 
     while self.is_listening: 
      cmd = ['application.exe'] 
      proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
      proc.wait() 
      data = "" 
      while True: 
       txt = proc.stdout.readline() 
        data = txt[5:].strip() 
        txt += data 

주 응용 프로그램이 종료되면 스레드는 여전히 절대로 발생하지 않은 사용자 작업을 기다리고 있습니다. 어떻게하면 정상적으로 종료 할 수 있습니까? GUI 응용 프로그램이 종료 된 후에도 application.exe 프로세스가 프로세스 목록에 계속 표시 될 수 있습니다. 모든 것을 개선하기위한 제안은 언제나 환영합니다.

감사

+2

왜'wx.Process' /'wx.Execute()'를 사용하지 않습니까? –

답변

2

1) ('시저'는 인스턴스 속성을 확인, 그래서 당신이 종료() 또는 죽일 호출 할 수 있습니다) 종료하기 전에 방법.

self.thread = threading.Thread(target=self.run, args=()) 
self.thread.setDaemon(True) 
self.thread.start() 

def run(self): 
    while self.is_listening: 
     cmd = ['application.exe'] 
     self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
     self.proc.wait() 
     data = "" 
     while True: 
      txt = self.proc.stdout.readline() 
      data = txt[5:].strip() 
      txt += data 

2) 중지 스레드에게 몇 가지 변수를 사용하는 대신 대기 (사용하는, 루프 (당신은 (설문 조사를 사용해야합니다))).

self.exit = False 
self.thread = threading.Thread(target=self.run, args=()) 
self.thread.setDaemon(True) 
self.thread.start() 

def run(self): 
    while self.is_listening: 
     cmd = ['application.exe'] 
     proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
     while proc.poll() is None or not self.exit: 
      pass 
     data = "" 
     while True: 
      if self.exit: 
       break 
      txt = proc.stdout.readline() 
      data = txt[5:].strip() 
      txt += data 

'atexit' module documentation

출구에서 일을 전화와 함께 당신을 도울 수 있습니다.

관련 문제