subprocess: returncode v. poll()

David wizzardx at gmail.com
Thu Sep 20 17:24:13 EDT 2007


On 9/20/07, 7stud <bbxx789_05ss at yahoo.com> wrote:
> On Sep 20, 1:25 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
> > On Sep 20, 1:17 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
> >
> >
> >
> > > Hi,
> >
> > > What is the difference between:
> >
> > > 1) getting the returncode directly from the subprocess object
> > > 2) calling poll() on the subprocess object?
> >
> > > Here is an example:
> >
> > > import subprocess
> >
> > > p = subprocess.Popen("ls", stdout=subprocess.PIPE)
> > > print p.returncode
> > > print p.poll()
> > > print
> >
> > > print p.stdout.read()[:5]
> > > print
> >
> > > print p.returncode
> > > print p.poll()
> > > print p.returncode
> >
> > > --output:--
> > > None
> > > None
> >
> > > 10tes
> >
> > > None
> > > 0
> > > 0
> >
> > Hmm....after a little more testing, I don't think returncode
> > dynamically updates:
> >
> > import subprocess
> > import time
> >
> > p = subprocess.Popen("ls", stdout=subprocess.PIPE)
> >
> > print p.returncode
> > time.sleep(5)
> > print p.returncode
> > time.sleep(2)
> > print p.returncode
> >
> > print p.stdout.read()[:5]
> > print p.returncode
> >
> > --output:--
> > None
> > None
> > None
> > 10tes
> > None
>
> ...but then when is p.returncode set?  And what good is it?

AFAICT p.returncode is only set by Popen methods. It doesn't
automatically get set by the OS internals that manage the actual
process.

One place where it is useful is when using Popen's communicate()
method, which returns (stdout,stderr), but not the return code. Also
it lets you call poll() but not have to save poll()'s return value.



More information about the Python-list mailing list