473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

popen2

Guy
Hi

I was given an exstremly useful answer to a problem that I had in
windows. I'm relativly new with python, but find it exstremly useful.
I'm creating a script to run test files and record output, the script
already works on win32 and I've made a telnet procedure which works
quite well when you want to do the testing on one of the many unix
boxes (but its very slow). What I want to do now is run the script on
a unix system, and later on, create a rsh function to run the tests on
different unix boxes which have not got python on them.

The following snippet of code is what I use on win32. just example
sets some env vars and executes an exe, collects and processes the
output. :

output, input = popen2("cmd.exe")
# Sets the enviroment.
input.write("set LI_DIR=value\n")
input.write("set LI_DIR_ALT=value\n")
input.write("set LI_ARCH=value\n")
input.write("set LI_PRECISION=value\n")
input.write("set LI_PERFORMANCE=value\n")
input.write("set LI_LINKING=\n")
input.write("set LI_ARCHFLAG=\n")
input.write("set LI\n")

# Writes commands to it.
input.write(f_mwtest_exe_str + " " + f_mwtest_args + " " +
os.path.dirname(f_testlwc_str) + " " + f_testlwc_str + "\n")

input.write("exit\n")

while 1:
text = output.readline()
if text:
process text line
else:
break:

The unix equivalant is something like this :

from popen2 import popen2
output, input = popen2("csh")
# Sets the enviroment.
input.write("setenv DISPLAY doors:0.0\n")

input.writeinput.write(f_mwtest_exe_str + " " + f_mwtest_args + " " +
os.path.dirname(f_testlwc_str) + " " + f_testlwc_str + "\n")

print "finished"
process the output from the test file.
The problem is at the moment I can't get the process to stop and wait
until the exe has finished executing, the python script executes
everything and then the test file is executed afterwards. I'm thinking
that there must be a switch on the csh or sh command to stop this
happening but I don't know what it is. The unix machines that I'm
working on are all different plat types, so the platform type proberly
won't help you.

NOTE the commands above will be a lot more varied, and are not just
used to set the enviroment up.

Any help would be of great value to me
TIA

Guy
Jul 18 '05 #1
1 7115
In article <f3*************************@posting.google.com> ,
gu*********@Machineworks.com (Guy) wrote:
The unix equivalant is something like this :

from popen2 import popen2
output, input = popen2("csh")
# Sets the enviroment.
input.write("setenv DISPLAY doors:0.0\n")

input.writeinput.write(f_mwtest_exe_str + " " + f_mwtest_args + " " +
os.path.dirname(f_testlwc_str) + " " + f_testlwc_str + "\n")

print "finished"
process the output from the test file.
The problem is at the moment I can't get the process to stop and wait
until the exe has finished executing, the python script executes
everything and then the test file is executed afterwards. I'm thinking
that there must be a switch on the csh or sh command to stop this
happening but I don't know what it is. The unix machines that I'm
working on are all different plat types, so the platform type proberly
won't help you.


Try something like
status = input.close()
result = output.read()
if status is None:
print 'success:', repr(result)
else:
print 'error exit', status, repr(result)

The close() is the important part:

1. flush your buffer so data actually goes to the shell
2. closes the shell's input so it doesn't stay around
waiting for more.
3. returns exit status for the shell process, or None
if the exit status is 0. (This won't work so well
for rsh.)

While I'm at it, I would recommend that you use "sh" instead
of "csh", because in the end it will give you less grief with
bugs and misfeatures. "setenv x v" -> "x=v export x" if you
do this. I would also recommend popen2("sh 2>&1") so that
the piped output includes error/diagnostic output.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #2

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

Similar topics

1
by: A. Lloyd Flanagan | last post by:
OK, I've got a weird one I haven't been able to figure out yet. Admittedly I haven't had time to dig into the library source, but this behavior certainly doesn't seem right. Here's a test case: ...
1
by: | last post by:
This could possibly be a bug, but I don't understand it fully so I'm posting here first. Searching the list told me other people are having this problem too. I have created a class which...
4
by: P | last post by:
I've written a couple of apps that required running a command and grabbing the output, and I've found the existing interfaces problematic for this. I think the proliferation of functions and...
1
by: Vivien Mallet | last post by:
Hello, I use popen2.Popen4 and I experienced problems with it. Let me show you with an example. The following script is called "lines" (it prints lines of 'X'): ---------------------...
1
by: Magnus Lycka | last post by:
I'm trying to read standard out in a process started with popen2 in a non-blocking way. (Other good ways of doing this than the one I tried are appreciated.) I've tried to dumb down my code to...
9
by: Martin P. Hellwig | last post by:
Hi all, I was doing some popen2 tests so that I'm more comfortable using it. I wrote a little python script to help me test that (testia.py): --------------------------------- someline =...
3
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out...
1
by: mikem76 | last post by:
Is there a way to get the process id when starting a process using os.popen2 or os.popen3 on linux? Mike
1
by: diego | last post by:
I'm trying to understand how popen2 works. Found in this group, that popen2.popen2 can cause trouble so i chose win32pipe.popen2. have a look a the listing of 2 files: ekmain.py: **************...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.