I had no problems with the code and technique below on MS-Win but on
Linux application is getting crashed.
I need to execute two external commands in an order and capture their
output. Once processes are launched, control should immediately come
back to GUI so that it should be responsive instead of waiting for
process two finish.
Process one is merely taken 0.5-1.0 second to finish where as process
2 is taking couple of seconds. Until process 1 is not finished process
2 should not start. For this I have used
wx.EXEC_SYNC for prcess1 and wx.EXEC_ASYNC for process 2.
The process class:
class Process(wx.Process):
""""""
def __init__(self, sync=False):
wx.Process.__init__(self)
self.timer = wx.Timer(self)
self.Redirect()
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.sync = sync
self.pid = None
def Start(self, cmd):
""""""
if self.sync:
self.pid = wx.Execute(cmd, wx.EXEC_SYNC, self)
else:
self.pid = wx.Execute(cmd, wx.EXEC_ASYNC, self)
self.timer.Start(200)
def Stop(self):
""""""
if self.pid != None:
try:
self.Kill(self.pid, sig=wx.SIGKILL)
time.sleep(0.5)
except:
print "Unable to stop"
def OnTerminate(self, pid, status):
""""""
# First empty pending outputs then stop the timer.
self.OnTimer(None)
self.timer.Stop()
self.pid = None
def OnTimer(self, event):
""""""
stream = self.GetErrorStream()
if stream.CanRead():
text = stream.read().strip()
# show text
calling code:
if self.process1 != None:
process1.Stop()
self. process1 = Process(sync=True)
self.process1.Redirect()
self.process1.Start(cmd1)
# prepare cmd and data for process 2 here
if self.process2 != None:
self.process2.Stop()
self.process2 = Process()
self.process2.Redirect()
try:
self.process2.Start(cmd2)
except Exception, e:
# show error msg here.
On Linux, first time both the process are executing, when executing
again,
if self.process1 != None:
process1.Stop()
This is causing application to crash without any warning or error.
AFAIK this is the easiest
way to manage the job but are their any other safe methods available?
Or there is something seriously wrong in the code above?
Prashant
#---- System Information ----#
GUI2Exe Version: 0.5.0
Operating System: Linux 2.6.31-14-generic i686
Python Version: 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11)
[GCC 4.4.1]
wxPython Version: 2.8.10.1 (gtk2-unicode)
wxPython Info: (__WXGTK__, wxGTK, unicode, gtk2, wx-assertions-off,
SWIG-1.3.29)
Python Encoding: Default=UTF-8 File=UTF-8
wxPython Encoding: utf-8
System Architecture: 32bit i686
Byte order: little
Frozen: False
#---- End System Information ----#