<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.3527" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009>Hi,</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>I have problems
understanding the subprocess.Popen object. I have a iterative calculation in a
process running and I want to pipe the output (stdout) from this
calculation to a Python script.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>Let me include a
simple code that simulates the calculating process:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>/* This code
simulates a big iterative calculation */</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>#include
<stdio.h><BR>#include <math.h></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>int
main()<BR>{<BR> float val[2] = { M_PI, M_E };<BR> int
i;<BR> <BR> for ( i = 0; i < 2 i++) {<BR> sleep( 15
); /* It's a hard calculation. It take 15 seconds
*/<BR> printf("Value: %5.6f\n", val[i] );<BR> fflush(
stdout );<BR> }<BR> return 0;<BR>}</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>let's compile this
to mycalc: gcc -o mycalc calc.c ... (untested code)</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>In C I have this
code which starts the mycalc process and handles the output from
it:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>#include
<stdio.h><BR>#include <assert.h><BR>#define BUF_SIZE
256</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>int
main()<BR>{<BR> FILE *pip;<BR> char
line[BUF_SIZE];</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009> <BR> pip
= popen("mycalc", "r");<BR> assert( pip != NULL );</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009> while ( fgets(
line, BUF_SIZE, pip )) {<BR> printf( "Hello; I got: %s \n", line
);<BR> fflush( stdout );<BR> }<BR> pclose( pip
);<BR> return 0;<BR>}<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=690323910-07052009>How can I make such
while-loop in Python? I assume I should use subprocess.Popen(), but I can't
figure out how?</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009>-Øystein</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=690323910-07052009></SPAN></FONT> </DIV><p></p><p><font size=1>-------------------------------------------------------------------<br>The information contained in this message may be CONFIDENTIAL and is<br>intended for the addressee only. Any unauthorised use, dissemination of the<br>information or copying of this message is prohibited. If you are not the<br>addressee, please notify the sender immediately by return e-mail and delete<br>this message.<br>Thank you.</font size></BODY></HTML>