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

R

raocheng

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
===========================================
 
G

Gabriel Genellina

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.

The hard part is to determine WHEN the command has completed its
execution, because there is no indication of that. One way would be to set
a relatively uncommon prompt, and look for that string in stdout. You
can't read beyond that, else the code would block.
Another way is to use a separate thread to read from stdout/stderr, and
set a timeout; when no more output comes whithin the timeout, you assume
the command has finished.

The code below uses the first approach, changing the prompt to <$$$>\r\n.
I've tested it on Windows only, but should work on Linux too with some
minor modifications.

import subprocess
from os import getenv

# Windows only, this is to determine the shell in use
# Linux users may try with getenv("SHELL", "sh")
shell = getenv("COMSPEC", "cmd")
p = subprocess.Popen(shell, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

expected = '<$$$>\r\n'
cmds = ['prompt $L$$$$$$$G$_','date /t','cd','dir','ipconfig /all']
# The first command above sets an uncommon prompt, ending with a newline.
# On Linux use PS1=whatever, but make sure it ends in \n and set the
# expected variable accordingly.

for cmd in cmds:
print ">>>",cmd
p.stdin.write('%s\n' % cmd)
while True:
line = p.stdout.readline()
if line.endswith(expected): break
print line,

print "Waiting for subshell to terminate"
p.stdin.write("exit\n")
p.wait()
print "Done"
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of raocheng
Sent: Friday, January 18, 2008 6:31 AM
To: (e-mail address removed)
Subject: How to use only a sub shell to execute many commands in python

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


Have each command write to a unique temp file.
Create temp files in python
cmd = 'date > /tmp/date_temp 2>&1 ; uptime > /tmp/uptime_temp
2>&1; ...'
execute cmd
for file in tempfiles:
...

You can also get the return value of each command
cmd = 'date > /tmp/date_temp 2>&1; echo $? >> /tmp/date_temp;
uptime > /tmp/uptime_temp 2>&1; echo $? >> ...'




*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top