473,503 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

os.popen3 hangs in Windows XP SP1, SP2. Python 2.5 & 2.4. Consistent test case.

Hi all,

I have a consistent test case where os.popen3() hangs in Windows. The
system hangs when retrieving the lines from the child process stdout.
I know there were several reports related to os.popen3() hanging under
Windows in this group before.

I stumbled on a case where a piece of code works in some occasions and
hangs consistently given different data. It performs exactly the same
on 3 different computers running Windows:

- computer 1: XP, SP1, Python 2.4.3
- computer 2: XP, SP2, Python 2.4.3
- computer 3: XP, SP2, Python 2.5

I know that a bug
(http://sourceforge.net/tracker/index...70&atid=105470)

related to popen3 hanging was opened and closed as 'not-a-bug'.
However I think that the test case I have is not a application problem
(a deadlock) and is a true problem with popen3() under Windows (but I
could be wrong.)

What I have is:

- a test script: tpopen.py.
- It uses nosetests.py to execute unit tests: it runs:
- test_roman.py, a test script for :
- roman.py, a modified version of Mark Pilgrims's roman.py
module, I used to test nosetests.
#--------------
The test case I have is a short program that invokes nosetests to run a
test_roman.py:

- The script topen.py is executed on the command line with::

topen test_roman

- topen.py executes

stdin, stdout, stderr = os.popen3('nosetests --nocapture
test_roman')

- The program runs properly if it then executes the following code
right after the call to os.popen3()::

stderr_lines = list(stderr)
stdin.close()
stderr.close()
pgm_exit_code = stdout.close() or 0

- The program hangs when it tries to read stdout (**but only ** when it
is running nosetests for the test_roman.py). The code following the
call to os.popen3() is::

for line in stdout:
sys.stdout.write(line) # prints almost all nosetests
stdout lines except for some at the end!
# then it HANGS.
stderr_lines = list(stderr)
stdin.close()
stderr.close()
pgm_exit_code = stdout.close() or 0
Note that it also hangs if topen.py executes:

stdin, stdout, stderr = os.popen3('nosetests test_roman')

Or if I modify the print statements inside test_roman.py, the unit test
scrip executed by nosetests.py.
I also tried changing the order of the closing of the stream (like
doing stdin.close() right after the popen3() call). No impact.

Note that popen3() call does not hang if topen runs other unit test
scripts i have.

#---------------------

QUESTIONS:
- Can any one see a problem with the order of execution that could
cause a deadlock here (topen.py code posted below)?
- Should I post a new Python bug, providing the complete test code or,
re-open the above mentioned bug?


# tpopen.py
---------------------------------------------------------------

import sys
import os

#
-----------------------------------------------------------------------------
# p o p e n 3 ( ) -- Execute a popen3 command --
# ^^^^^^^^^^^^^^^
#
def popen3(command,
stdout_parser=None,
stderr_parser=None,
default_stdout_value=None,
default_stderr_value=None) :

pgm_exit_code = 0
stdout_value, stderr_value = default_stdout_value,
default_stderr_value
stdin, stdout, stderr = os.popen3(command)
stdin.close()
if stdout_parser:
stdout_value = stdout_parser(stdout)
if stderr_parser:
stderr_value = stderr_parser(stderr)
stderr.close()
pgm_exit_code = stdout.close() or 0
return (stdout_value, stderr_value, pgm_exit_code)
#
-----------------------------------------------------------------------------

cmd1 = 'nosetests ' + sys.argv[1]
cmd2 = 'nosetests --nocapture ' + sys.argv[1]

def print_stream(stream):
for line in stream:
sys.stdout.write(line)

print '--------- CMD 1 , no stdout print : does not hang --------'
stdout, stderr_lines, pgm_exit_code = popen3(cmd1, None, list)
print 'stderr_lines: ', stderr_lines
print 'program exit code : ', pgm_exit_code
print '--------- CMD 2 : HANGS after printing almost all stdout -'
stdout, stderr_lines, pgm_exit_code = popen3(cmd2, print_stream, list)
print 'stderr_lines: ', stderr_lines
print 'program exit code : ', pgm_exit_code
print '--------- CMD 1 , stdout print : HANGS -------------------'
stdout, stderr_lines, pgm_exit_code = popen3(cmd1, print_stream, list)
print 'stderr_lines: ', stderr_lines
print 'program exit code : ', pgm_exit_code

#
-----------------------------------------------------------------------------

--

Pierre Rouleau

Dec 11 '06 #1
2 2679
Pierre Rouleau wrote:
Hi all,

I have a consistent test case where os.popen3() hangs in Windows. The
system hangs when retrieving the lines from the child process stdout.
I know there were several reports related to os.popen3() hanging under
Windows in this group before.
I had a problem like this some time ago. But it was a problem that
would happen under any operating system.

If there is output on stdout and stderr, you will get a dead lock sooner
or later.

Example: The childprocess tries to write to stderr, and the
parent process reads from stdin. The buffer of stderr will
get full. The child will block. The parent will wait for ever.

See http://docs.python.org/lib/popen2-flow-control.html

My hint: Always use popen4

You can get dead locks with popen4, too. But only if you
write to pipe.tochild.

Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/ http://www.tbz-pariv.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: ni**************@thomas-guettler.de

Dec 12 '06 #2


On Dec 12, 10:11 am, Thomas Guettler
<guettli.use...@thomas-guettler.dewrote:
Pierre Rouleau wrote:
Hi all,
I have a consistent test case where os.popen3() hangs in Windows. The
system hangs when retrieving the lines from the child process stdout.
I know there were several reports related to os.popen3() hanging under
Windows in this group before.I had a problem like this some time ago. But it was a problem that
would happen under any operating system.

If there is output on stdout and stderr, you will get a dead lock sooner
or later.

Example: The childprocess tries to write to stderr, and the
parent process reads from stdin. The buffer of stderr will
get full. The child will block. The parent will wait for ever.

Seehttp://docs.python.org/lib/popen2-flow-control.html

My hint: Always use popen4

You can get dead locks with popen4, too. But only if you
write to pipe.tochild.
Thanks for replying Thomas.

The reason I was using popen3() is that I need to parse the error
stream and do something else with the stdout. My operation does not
need to have concurrent operations of the parent and child. I could
have used something file::

os.system('parent log_stdout.txt 2log_stderr.txt')

and then parse the 2 files. I just wanted to avoid using the files to
avoid having to have to deal with issues related to temporary file
names if there where several process instance of the code running
simultaneously.
Now, from the reading of the above link, this means that If I want to
be able to do what I want with pipes, avoiding deadlock means that:

- In the parent program, the code should be something that looks like:

stdin, stdout, stderr = os.popen3(command)
stderr_value = list(stderr)
stdout_value = list(stdout)
pgm_exit_code = stdout.close() or 0
stdin.close()
stderr.close()

An the above would work only if one stream is written by the child at a
time and stderr closed:

for whatever: print >sys.stderr, ' the error messages'
os.close(sys.stderr.fileno())
for someother: print 'other stdout info'

In my case, since I don't control the child program, I can assume that
it does not follow the required order. I am launching nosetests which
runs other test programs. I tried closing sys.stderr in the teardown
of my test script and that removed the deadlock but caused other
problems (because sys.stderr is used later by nosetests).

So, in the end, it looks like I really don't have any choice: if I want
to safely read both stdout and stderr in a way that is child program
agnostic: I must use temporary files (or maybe use something like
select). Right?

Thanks again!

--

Pierre Rouleau

Dec 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3097
by: | last post by:
This could possibly be a bug, but I don't understand it fully so I'm posting here first. Searching the list told me other people are having this problem too. I have created a class which...
13
4430
by: Russell E. Owen | last post by:
I'm trying to launch an application from Python 2.3 on Windows. The application is "ds9" (an image viewer), and is installed in C:\Program Files\ds9\ds9 On unix I just do: os.popen3("ds9") and...
1
1718
by: Kenneth Pronovici | last post by:
Hi, I have a problem with a popen2 pipe hanging partway through execution of a command. This happens in my function executeCommand() that is used for every "shell" command execution in my...
2
4139
by: Jakob Simon-Gaarde | last post by:
Follow-up on a thread from 1999 (see below) Well now it is 2005 and the operating system I'm using is Windows Server 2003, and I can still see that the same problem persists with: ...
1
1311
by: Fuzzyman | last post by:
Hello all, I may well post this a a bug on Monday (after testing with Python 2.3) - but I thought I'd post here to see if anyone has any ideas. The basic problem is that under Python 2.4 (and...
0
6715
by: Vijay | last post by:
Prep Courses for International Certifications, CSTE & CSQA & ISEB & ISTQB &Business Analyst & SOA Certifications in HYDERABAD. After receiving overwhelming response to our last 50+ batches, ...
4
2576
by: dumbkiwi | last post by:
I have written a script that uses the urllib2 module to download web pages for parsing. If there is no network interface, urllib2 hangs for a very long time before it raises an exception. I...
3
2039
by: Christoph Krammer | last post by:
Hello everybody, I need to get the different frames from a GIF image in my python script and want to use the giftopnm program from netpbm to get the frames and directly convert them to pnm...
1
2403
by: Christoph Krammer | last post by:
Hello everybody, I try to use an external OCR tool to convert some binary image data to text. The image is in one variable, the text should be converted to another. I use the following code: ...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
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
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7468
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...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.