Note that there are some explanatory texts on larger screens.

plurals
  1. POwxPython, capturing an output from subprocess in real-time
    primarykey
    data
    text
    <p>I'm working on application in wxPython which is a GUI for a command line utility. In the GUI there is a text control which should display the output from the application. I'm launching the shell command using subprocess, but I don't get any output from it until it has completed.</p> <p>I have tried several solutions but none of them seems to work. Below is the code I'm using at the moment (updated):</p> <pre><code> def onOk(self,event): self.getControl('infotxt').Clear() try: thread = threading.Thread(target=self.run) thread.setDaemon(True) thread.start() except Exception: print 'Error starting thread' def run(self): args = dict() # creating a command to execute... cmd = ["aplcorr", "-vvfile", args['vvfile'], "-navfile", args['navfile'], "-lev1file", args['lev1file'], "-dem", args['dem'], "-igmfile", args['outfile']] proc = subprocess.Popen(' '.join(cmd), shell=True, stdout=subprocess.PIPE, stderr.subprocess.PIPE) print while True: line = proc.stdout.readline() wx.Yield() if line.strip() == "": pass else: print line.strip() if not line: break proc.wait() class RedirectInfoText: """ Class to redirect stdout text """ def __init__(self,wxTextCtrl): self.out=wxTextCtrl def write(self,string): self.out.WriteText(string) class RedirectErrorText: """ Class to redirect stderr text """ def __init__(self,wxTextCtrl): self.out.SetDefailtStyle(wx.TextAttr()) self.out=wxTextCtrl def write(self,string): self.out.SetDefaultStyle(wx.TextAttr(wx.RED)) self.out.WriteText(string) </code></pre> <p>In particular I'm going to need the output in real-time to create a progress-bar.</p> <p>Edit: I changed my code, based on Mike Driscoll's suggestion. It seems to work sometimes, but most of the time I'm getting one of the following errors:</p> <blockquote> <p>(python:7698): Gtk-CRITICAL **: gtk_text_layout_real_invalidate: assertion `layout->wrap_loop_count == 0' failed</p> </blockquote> <p>or</p> <blockquote> <p>(python:7893): Gtk-WARNING **: Invalid text buffer iterator: either the iterator is uninitialized, or the characters/pixbufs/widgets in the buffer have been modified since the iterator was created. You must use marks, character numbers, or line numbers to preserve a position across buffer modifications. You can apply tags and insert marks without invalidating your iterators, but any mutation that affects 'indexable' buffer contents (contents that can be referred to by character offset) will invalidate all outstanding iterators Segmentation fault (core dumped)</p> </blockquote> <p>Any clues? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload