Thanks,
The example"Redirecting text from stdout to a wx.TextCtrl" at LongRunningTasks - wxPyWiki is something I was looking for.
Most of the examples are loop based on the links but in my case it’s not. Although I gave a try by modify the function “LongRunningProcess” with this code:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
my_process1 = subprocess.Popen(cmnd1, startupinfo=startupinfo, stdout=subprocess.PIPE)
print (my_process1.communicate()[0])
I think I am doing the same thing again which is causing an error when executing this code. This is the first time I am getting into Threads & Process and
I am honing my skills on these two topics. Mean while any solution would be greatly
appreciated.
Regards.
Prashant Saxena
···
----- Original Message ----
From: Robin Dunn robin@alldunn.com
To: wxpython-users@lists.wxwidgets.org
Sent: Tuesday, September 2, 2008 2:18:58 AM
Subject: Re: [wxpython-users] subprocess+wxWidgets
Prashant Saxena wrote:
Hi,
I am using subprocess module to execute a command and print results back.
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
my_process = subprocess.Popen(cmnd, startupinfo=startupinfo)
print repr(my_process.communicate()[0])
This code executes on pressing a button (wxPython). The problem is until
command is not done
and it’s results are not printed, program halts and button keep the
state of pushed.
Is there any way to avoid this? or If I can directly hook wx.TextCtrl to
read the output from process then
it would be great.
http://wiki.wxpython.org/LongRunningTasks
http://wiki.wxpython.org/Non-Blocking%20Gui
By waiting (in communicate()) for the process to complete you are
preventing control from returning to the MainLoop, so it is unable to
send any more events (like the paint event for the button.) Instead you
should periodically (EVT_TIMER or EVT_IDLE) come back and check the
status of the process and read any pending output from the process and
do whatever you need to with it, but don’t ever block returning to MainLoop.
–
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Hello,
I'm not using subprocess but I've derived a class from wx.Process. I think you can try to adapt this...
I hope it could help. Here is some peaces of my script:
class MyProcess(wx.Process):
def __init__(self, study):
wx.Process.__init__(self)
self.timer = wx.Timer(self)
self.Redirect()
self.Bind(wx.EVT_TIMER, self.OnTimer)
def Start(self, cmd):
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.GetInputStream()
if stream.CanRead():
text = stream.read()
# !!! Do what you want with the text !!!
Prashant Saxena wrote:
···
Thanks,
The example"Redirecting text from stdout to a wx.TextCtrl" at LongRunningTasks - wxPyWiki is something I was looking for.
Most of the examples are loop based on the links but in my case it's not. Although I gave a try by modify the function "LongRunningProcess" with this code:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
my_process1 = subprocess.Popen(cmnd1, startupinfo=startupinfo, stdout=subprocess.PIPE)
print (my_process1.communicate()[0])
I think I am doing the same thing again which is causing an error when executing this code. This is the first time I am getting into Threads & Process and
I am honing my skills on these two topics. Mean while any solution would be greatly appreciated.
Regards.
Prashant Saxena
----- Original Message ----
From: Robin Dunn <robin@alldunn.com>
To: wxpython-users@lists.wxwidgets.org
Sent: Tuesday, September 2, 2008 2:18:58 AM
Subject: Re: [wxpython-users] subprocess+wxWidgets
Prashant Saxena wrote:
> Hi,
>
> I am using subprocess module to execute a command and print results back.
>
> startupinfo = subprocess.STARTUPINFO()
> startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
> my_process = subprocess.Popen(cmnd, startupinfo=startupinfo)
> print repr(my_process.communicate()[0])
>
> This code executes on pressing a button (wxPython). The problem is until
> command is not done
> and it's results are not printed, program halts and button keep the
> state of pushed.
>
> Is there any way to avoid this? or If I can directly hook wx.TextCtrl to
> read the output from process then
> it would be great.
LongRunningTasks - wxPyWiki
Non-Blocking Gui - wxPyWiki
By waiting (in communicate()) for the process to complete you are
preventing control from returning to the MainLoop, so it is unable to
send any more events (like the paint event for the button.) Instead you
should periodically (EVT_TIMER or EVT_IDLE) come back and check the
status of the process and read any pending output from the process and
do whatever you need to with it, but don't ever block returning to MainLoop.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!