2009-04-07 6 views
0

wx.Process 서브 클래스를 만들려고하는데 사용자 정의 프로세스 시작 프로그램을 사용하여 stdout 스트림에서 수집 된 데이터로 메인 스레드로 이벤트를 다시 발생시킵니다. 이것은 일을하는 좋은 방법입니까? 여기 wx.Process에서 내 메인 스레드로 정보를 전파하는 가장 좋은 방법은 무엇입니까?

class BuildProcess(wx.Process): 
    def __init__(self, cmd, notify=None): 
     wx.Process.__init__(self, notify) 
     print "Constructing a build process" 
     self.Bind(wx.EVT_IDLE, self.on_idle) 
     self.Redirect() 
     self.cmd = cmd 
     self.pid = None 

    def start(self): 
     print "Starting the process" 
     self.pid = wx.Execute(self.cmd, wx.EXEC_ASYNC, self) 
     print "Started." 

    def on_idle(self, evt): 
     print "doing the idle thing..." 
     stream = self.GetInputStream() 
     if stream.CanRead(): 
      text = stream.read() 
      wx.PostEvent(self, BuildEvent(EVT_BUILD_UPDATE, self, data=text)) 
      print text 

    def OnTerminate(self, *args, **kwargs): 
     wx.Process.OnTerminate(self, *args, **kwargs) 
     print "Terminating" 

BuildEvent

wx.PyEvent의 사용자 정의 서브 클래스입니다. 프로세스가 시작, 실행 중이고 올바르게 종료되지만 내 유휴 이벤트에 바인드 했음에도 불구하고 내 on_idle 기능이 실행되지 않습니다.

답변

1

목적은 다른 프로세스의 메소드를 호출하는 것이 아니라, 프로세스가 실행될 때 주기적으로 실행되는 "update"이벤트를 통해 다른 프로세스의 stdout을 부모 프로세스로 다시 리디렉션하는 것입니다.

하나의 솔루션은 우리가 우리의 작업을 할 EVT_IDLE에 의존하지 않도록 주기적으로 프로세스의 출력 스트림을 폴링 wx.Timer을 사용하는 것입니다

class BuildProcess(wx.Process): 

    def __init__(self, cmd, notify=None): 
     wx.Process.__init__(self, notify) 
     self.Redirect() 
     self.cmd = cmd 
     self.pid = None 
     self.timer = wx.Timer(self) 
     self.Bind(wx.EVT_TIMER, self.on_timer) 

    def start(self): 
     wx.PostEvent(self, BuildEvent(EVT_BUILD_STARTED, self)) 
     self.pid = wx.Execute(self.cmd, wx.EXEC_ASYNC, self) 
     self.timer.Start(100) 

    def on_timer(self, evt): 
     stream = self.GetInputStream() 
     if stream.CanRead(): 
      text = stream.read() 
      wx.PostEvent(self, BuildEvent(EVT_BUILD_UPDATE, self, data=text)) 


    def OnTerminate(self, *args, **kwargs): 
     print "terminating..." 
     stream = self.GetInputStream() 
     if stream.CanRead(): 
      text = stream.read() 
      wx.PostEvent(self, BuildEvent(EVT_BUILD_UPDATE, self, data=text)) 
     if self.timer: 
      self.timer.Stop() 
     wx.PostEvent(self, BuildEvent(EVT_BUILD_FINISHED, self)) 
(I 화재하는데 문제 EVT_IDLE 있었다)

이 방법을 사용하면 출력 스트림을 100ms마다 읽고 패키지로 만들어 빌드 이벤트로 제공됩니다.

0

wxProcess 문서를 보는 것으로부터 나는 그렇게 생각하지 않습니다. wxProcess는 현재 프로세스의 자식으로 실행되는 새로운 별도의 프로세스를 생성합니다. 이러한 프로세스에서 메시지에 연결된 메소드를 실행하는 것은 불가능합니다.

유휴 이벤트를 기본 스레드의 함수 또는 메소드에 연결할 수 있습니다.

또는 wxThread 클래스는 실제로 사용하려는 클래스입니다.

관련 문제