See Sharp Cuts
See Sharp Cuts

Reputation:

Running multiple processes and capturing the output in python with pygtk

I'd like to write a simple application that runs multiple programs and displays their output in multiple terminal (style) windows. In addition, I want to be able to read the stdout/stderr of these processes and search for keywords in the output.

I've tried implementing this two ways in python, the first using subprocess.Popen and the second using vte (python-vte).

I've only gotten Popen to work w/ polling. I have to constantly check to see if the processes have data to be read, read the data, and then send it to my TextArea. It's been recommended to use gobject.io_add_watch() instead, but whenever I try that my program hangs on the second call to io_add_watch--it's like it can only handle one file descriptor at a time.

vte works great but I haven't found a reliable way to capture the output. You can get a callback when the cursor moves and then screen scrape w/ get_text(), but I've already run into cases where these programs I'm viewing generate an obscene about of tty in one go and then it's off the screen. There doesn't appear to be a callback that contains new text to be added to the window.

Any ideas?

Upvotes: 1

Views: 1488

Answers (3)

S.Lott
S.Lott

Reputation: 391852

You want to use select to monitor the pipes from your subprocesses. It's better than polling.

Upvotes: 0

Jon Cage
Jon Cage

Reputation: 37490

If you go with igkuk's suggestion, I got some good advice on watching files for changes in a related question. That worked pretty well for me (I was watching a log file for changes).

Upvotes: 1

igkuk7
igkuk7

Reputation: 339

I did something similar to this using the subprocess.Popen. For each process I actually ended up redirecting the stdout and stderr to a temporary file, then periodically checking the file for updates and dumping the output into a TextView.

The reason for not using a pipe to the process was that the processes themselves were volatile and prone to segfaults. When that happened I sometimes lost data between the last read and the segfault (which was the most needed data to determine the cause of the segfault).

As it turned out, sometimes I'd want to save the output from a specific process, so this method worked well for me.

Upvotes: 1

Related Questions