473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 17407
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Will | last post by:
I have this bit of code: #!/usr/bin/python import subprocess calc = subprocess.Popen("dc", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) max = 5
0
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run =...
0
by: Corey Wallis | last post by:
Dear All, I'm currently working on a project that needs to collect the output of the JHOVE application. More information about the application is available at this website: ...
2
by: Jim | last post by:
Hello, I need a program that will traverse a directory tree to ensure that there are unix-style line endings on every file in that tree that is a text file. To tell text files from others I...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
3
by: jakub.hrozek | last post by:
Hello, My program uses the subprocess module to spawn a child and capture its output. What I'd like to achieve is that stdout is parsed after the subprocess finishes, but anything that goes to...
4
by: grayaii | last post by:
There are so many threads on this subject, but I ran across a situation on Windows that I can't figure out. I'm trying to run this little command-line exe and when I launch like this, it hangs:...
2
by: rparimi | last post by:
I am trying to redirect stderr of a process to a temporary file and then read back the contents of the file, all in the same python script. As a simple exercise, I launched /bin/ls but this doesn't...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.