473,657 Members | 2,395 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=Fa lse, I use popen2.Popen4 so that stderr and stdout are
intermingled. If ignoreStderr=Tr ue, I use popen2.Popen3 with
capturestderr=T rue 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.c lose() 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=Fa lse,
ignoreStderr=Fa lse, 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(a rgs)
if ignoreStderr:
pipe = popen2.Popen3(f ields, capturestderr=T rue)
else:
pipe = popen2.Popen4(f ields)
pipe.tochild.cl ose()
while True:
line = pipe.fromchild. readline()
if not line: break
if returnOutput: output.append(l ine)
if outputFile is not None: outputFile.writ e(line)
outputLogger.in fo(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 1721
On Wed, Mar 09, 2005 at 06:17:50AM -0000, Donn Cave wrote:
Quoth Kenneth Pronovici <pr******@skyja mmer.com>:
...
| If ignoreStderr=Fa lse, I use popen2.Popen4 so that stderr and stdout are
| intermingled. If ignoreStderr=Tr ue, I use popen2.Popen3 with
| capturestderr=T rue 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.c lose() 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
4453
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 close the returned files and all is good. (I'm not trying to communicate with the program via popen3 and so had been using os.spawnlp, but that doesn't exist on Windows.)
2
4161
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: win32pipe.popen2() win32pipe.popen3() win32pipe.popen4() while win32pipe.popen() does almost what you want.
2
2686
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 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:
0
1604
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 was upgraded to Debian sarge. Now the call to select() always takes about 13 seconds before returning the first time. The delay only occurs when the function is running within a CGI program
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4152
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.