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

Kragen Sitaker kragen at pobox.com
Tue May 21 11:10:18 EDT 2002


Eric Brunel <eric.brunel at pragmadev.com> writes:
> On Unix, there's a simple way: use the Popen classes instead of the popen 
> functions and use the "poll" method:
> 
> import popen2
> pager_cmd = 'less'
> p = popen2.Popen4(pager_cmd)
> if p.poll() != -1:
>   print "%s doesn't exist" % pager_cmd

I think this is a race condition.  Under some circumstances ---
possibly only under heavy CPU load, heavy disk activity, heavy memory
use, heavy network activity, or some combination of the above --- you
will poll() before the child process does an exec() (or at least
before it handles the error from the exec() and quits), or after the
user has manually told the pager to exit.




More information about the Python-list mailing list