How to use only a sub shell to execute many commands in python

raocheng rc.china at gmail.com
Fri Jan 18 12:31:25 CET 2008


Please see the following code.
Suppose I have many shell commands to be executed. And I don't want to
fork a sub shell for each command(eg: status,output =
commands.getstatusoutput(cmd)) because it is too expensive. I want to
use only one sub shell to execute all these commands and want to get
each command's output. How can I accomplish this task ? Thanks in
advance.

===========================================
#!/usr/bin/env python
import os
fi, fo = os.popen2(
'''
while read line
do
  eval $line
done
''',   't')

#Suppose I have many commands to execute, but I don't want to fork a
sub shell for each command
cmds = ['date','uptime','pwd','ls -rltF','who']

for cmd in cmds:
    #pseudocode
    fi.executeCmd(cmd)
    output = fo.readResult()

    print output
===========================================



More information about the Python-list mailing list