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

Home Posts Topics Members FAQ

Popen3 and capturestderr

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 program. Source code for this
function is below. My development environment is Debian unstable with
Python 2.3.5.

Within executeCommand(), output of every executed command is always
logged using a Python logger. If outputFile is passed in, then output
will also be written into the outputFile, which might have been created
using open(), gzip.GzipFile() or bz2.BZ2File(), etc. This functionality
exists so I have an equivalent of command-line redirection of stdout,
i.e. command > file.

If ignoreStderr=False, I use popen2.Popen4 so that stderr and stdout are
intermingled. If ignoreStderr=True, I use popen2.Popen3 with
capturestderr=True so stderr doesn't appear in the output. This
functionality exists so I have an equivalent of command-line redirection
of stderr, i.e. command 2>/dev/null.

I am using popen2 rather than popen because I want to avoid all of the
issues around shell interpolation of arguments, etc.

This whole thing has been working pretty well for a while. Then lately,
I started using it on commands that produce a large amount of output,
and it's begun to hang partway through execution. A good way to
reproduce this behavior on my system is to use command "ls -laR /". The
function hangs whever the output file reaches a certain size (around 20
MB). When I interrupt the program, the stack trace shows that it's
stuck at pipe.fromchild.readline().

After some digging, I've decided that this behavior probably occurs
because I am ignoring the pipe.childerr file object. Indeed, if I call
pipe.childerr.close() right after opening the pipe, my "ls" command that
had been hanging completes normally. However, other commands which
actually attempt to write to stderr don't seem to like this very much.

What is the right way to discard stderr when working with a pipe? I
want to consistently throw it away, and I don't see a good way to do
this with the popen2 implementation.

Thanks for the help,

KEN

------------------------------- snip -------------------------------

def executeCommand(command, args, returnOutput=False,
ignoreStderr=False, outputFile=None):
"""
@param command: Shell command to execute in a list like [ "ls", ]
@param args: List of arguments to the command, like [ "-laR", "/", ]
@param returnOutput: Indicates whether to return the output of the command
@param outputFile: File object that all output should be written to.
@return: Tuple of C{(result, output)}.
"""
output = []
fields = command[:]
fields.extend(args)
if ignoreStderr:
pipe = popen2.Popen3(fields, capturestderr=True)
else:
pipe = popen2.Popen4(fields)
pipe.tochild.close()
while True:
line = pipe.fromchild.readline()
if not line: break
if returnOutput: output.append(line)
if outputFile is not None: outputFile.write(line)
outputLogger.info(line[:-1])
if returnOutput:
return (pipe.wait(), output)
else:
return (pipe.wait(), None)

------------------------------- snip -------------------------------

--
Kenneth J. Pronovici <pr******@ieee.org>
Jul 18 '05 #1
1 1718
On Wed, Mar 09, 2005 at 06:17:50AM -0000, Donn Cave wrote:
Quoth Kenneth Pronovici <pr******@skyjammer.com>:
...
| If ignoreStderr=False, I use popen2.Popen4 so that stderr and stdout are
| intermingled. If ignoreStderr=True, I use popen2.Popen3 with
| capturestderr=True so stderr doesn't appear in the output. This
| functionality exists so I have an equivalent of command-line redirection
| of stderr, i.e. command 2>/dev/null.
...
| After some digging, I've decided that this behavior probably occurs
| because I am ignoring the pipe.childerr file object. Indeed, if I call
| pipe.childerr.close() right after opening the pipe, my "ls" command that
| had been hanging completes normally. However, other commands which
| actually attempt to write to stderr don't seem to like this very much.
|
| What is the right way to discard stderr when working with a pipe? I
| want to consistently throw it away, and I don't see a good way to do
| this with the popen2 implementation.

Right, popen2 gives you about 3 options, out of probably dozens that
you could get with shell redirections. On the other hand, the source
is available, and Python is an OOP language, so I assume there is no
reason you can't make a derived class that does just what you want.
In the present case I guess that would mean something like
null = os.open('/dev/null', os.O_RDWR)
os.dup2(null, 0)
os.dup2(null, 2) (depending)
os.close(null)
along with other stuff you can just copy from Popen4.


Ah... ok, subclassing is an option I hadn't considered. I'll give that
a whirl and see whether I can make it work.

KEN

--
Kenneth J. Pronovici <pr******@ieee.org>
Personal Homepage: http://www.skyjammer.com/~pronovic/
"They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety."
- Benjamin Franklin, Historical Review of Pennsylvania, 1759
Jul 18 '05 #2

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

Similar topics

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...
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: ...
2
2679
by: Pierre Rouleau | last post by:
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...
0
1589
by: Jonathan Mark | last post by:
hi all... I wrote a seemingly simple function (below) to use Popen3() and select() to run a program and capture its exit status, stdout output, and stderr output. It worked fine until the box...
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
7353
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
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...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.