472,145 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

subprocess: returncode v. poll()

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

Sep 20 '07 #1
4 17110
On Sep 20, 1:17 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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
Sep 20 '07 #2
On Sep 20, 1:25 pm, 7stud <bbxx789_0...@yahoo.comwrote:
On Sep 20, 1:17 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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?

Sep 20 '07 #3
On 9/20/07, 7stud <bb**********@yahoo.comwrote:
On Sep 20, 1:25 pm, 7stud <bbxx789_0...@yahoo.comwrote:
On Sep 20, 1:17 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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.
Sep 20 '07 #4
On Sep 20, 3:24 pm, David <wizza...@gmail.comwrote:
On 9/20/07, 7stud <bbxx789_0...@yahoo.comwrote:
On Sep 20, 1:25 pm, 7stud <bbxx789_0...@yahoo.comwrote:
On Sep 20, 1:17 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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.
I didn't understand your post when I first read it, but after looking
at my output again, and rereading your post, I get it now. In case
someone else doesn't understand it: returncode is not set by the
child process--ever. return code starts off with a default value of
None, and it remains None until you call a method in the subprocess
module, like poll() or wait(). Those methods set and then return
returncode. As a result, if you want to know what the status of the
child process is, you have to call either poll() or wait().

Sep 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Christoph Haas | last post: by
reply views Thread by Corey Wallis | last post: by
12 posts views Thread by bhunter | last post: by
3 posts views Thread by jakub.hrozek | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.