[Zope] why I don't use popen2 any more

Dennis Nichols nichols@tradingconnections.com
Wed, 20 Dec 2000 19:51:12 -0600


In the for what it's worth department...

Using Linux (RH 6.2), Zope 2.2.2, Python 1.5.2

I wanted to run a unix command from my External Method that I wrote using 
Python. Digging around in Zope archives and other sources, I came up with a 
method that worked, of which this is a fragment:

     import popen2

         (r, w) = popen2.popen2('somecommand')
         w.write(commandinput)
         w.close()
         s = r.read()
         r.close()

However, this had the nasty side effect of leaving <defunct> processes 
around. I discovered one instance on the web of someone having this same 
problem using Python but not Zope-related. No solution was given.

I *think* the really bad thing about these processes (called zombies?) is 
that they may have certain resources, such as files, held open. And they 
are said to stay around until the process that created them dies itself.

The fix seems to be easier than the original solution. Replace all of the 
above with

     import commands

         s = commands.getoutput('echo ' + commandinput + ' | somecommand')

Presto, no more defunct processes. Hopefully a more stable Zope. Keen 
observers will note the security risk if commandinput contains arbitrary 
input from users. Wasn't true for me but it might be for you.

A curiosity: the description in Python Essential Reference says getoutput 
uses os.popen2() to execute the command, but I was too lazy^H^H^H^H busy to 
investigate further.


--
Dennis Nichols
nichols@tradingconnections.com