How to know if a popen*() was successful?

Fernando Pérez fperez528 at yahoo.com
Mon May 20 20:38:56 EDT 2002


I'm failing at detecting whether a process has indeed been opened by a popen4 
call. Consider the following code:

pager_cmd = 'less'
pager,shell_out = os.popen4(pager_cmd,'w')

How can I tell if less was indeed opened?

- reading shell_out doesn't work because if there are no error messages 
(success) it just hangs. I guess I could put a signal handler around it and 
trip an alarm but it sounds like major overkill and I'd like to know if 
there's a cleaner way.

- I can't just start writing to the pipe and test for a broken pipe because 
when the user legally quits less (if it opened ok), a broken pipe exception 
is also triggered (which I'm already catching downstream).

So, is there a way to determine immediately after the popen4() call if the 
pipe to less did open ok? The problem is that it _appears_ to open (pager is 
a file object even if less is not installed), and the error conditions only 
seem to be tripped further downstream.

Any help greatly appreciated.

Cheers,

f.

ps. I'm testing on linux, but this code needs to be multiplatform, hence my 
need to check ok. If less isn't present, I default to a python-written dumb 
pager for platforms like windows.



More information about the Python-list mailing list