Re: subprocess.Popen howto?

2009-05-08 Thread norseman


Øystein ;
Down below: change yourPy.py  to | yourPy.py
I just noticed the typo.

Sorry

Steve

norseman wrote:

Øystein Johansen (OJOHANS) wrote:

Hi,
 
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.
 
Let me include a simple code that simulates the calculating process:

/* This code simulates a big iterative calculation */
#include stdio.h
#include math.h
 
int main()

{
 float val[2] = { M_PI, M_E };
 int i;
 
 for ( i = 0; i  2 i++) {

  sleep( 15 );   /* It's a hard calculation. It take 15 seconds */
  printf(Value: %5.6f\n, val[i] );
  fflush( stdout );
 }
 return 0;
}
 
let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code)
 
In C I have this code which starts the mycalc process and handles the 
output from it:
 
#include stdio.h

#include assert.h
#define BUF_SIZE 256
 
int main()

{
 FILE *pip;
 char line[BUF_SIZE];
 
 pip = popen(mycalc, r);

 assert( pip != NULL );
 
 while ( fgets( line, BUF_SIZE, pip )) {

  printf( Hello; I got: %s \n, line );
  fflush( stdout );
 }
 pclose( pip );
 return 0;
}
How can I make such while-loop in Python? I assume I should use 
subprocess.Popen(), but I can't figure out how?
 
-Øystein
 
 


---
The information contained in this message may be CONFIDENTIAL and is
intended for the addressee only. Any unauthorised use, dissemination 
of the

information or copying of this message is prohibited. If you are not the
addressee, please notify the sender immediately by return e-mail and 
delete

this message.
Thank you.




--
http://mail.python.org/mailman/listinfo/python-list



If you don't like a lot of typing that obscures the process,
take a look at os.Popen2  Pg.39 or so in Lib.pdf for 2.5.2
In this case - the popen3 is probably your best bet.

I took a test run on subprocess a few months ago. My review:
excessive typing to accomplish the simple.  BlackBox stuff is supposed 
to be short and sweet.


BlackBox was the term applied to drivers and couplers back when.
Back when: one started by writing a subroutine that got used.
   next one got to write a program
   next one got to write an application
   then a BlackBox
   from there one could graduate to OS
That was back when!


to repeat from another of my postings:
---
processPython 2.5.2 (r252:60911, Mar  4 2008, 10:40:55)
[GCC 3.3.6] on linux2
Type help, copyright, credits or license for more information.

  import os
  AbWd = os.spawnlp( os.P_WAIT,abiword,abiword,)

The P_WAIT stops python until the program (abiword in this case) 
completes.  The  at the end are for tokens to be given to the program 
and yes - contrary to manual, the program MUST be there TWICE (complete 
with any path needed).


for windows this works:
(for cut and paste from cmd.exe, see:
  Re: Copy  Paste in a Dos box  from norseman  05/06/2009 4:28PM
)

Python 2.5.1 ... on win32
  import os
  result = os.spawnl( os.P_WAIT, d:\\winmcad\\mcad,)

Runs the program mcad. Returns to python when mcad exits.
---


In your case:  substitute ...your_compiled_c_program,  yourPy.py)
at the obvious places.

In case this isn't clear;
  method 1:  py1.py starts compiled.c which redirects to yourPy.py
  method 2:  py1.py uses os.popen3(compiled.c...)  the two work it out.
In either case the receiver at the end stays open until completion so 
sleep() things are not needed.  (may need to test for EOL and EOT)


HTH

Steve
norse...@hughes.net
--
http://mail.python.org/mailman/listinfo/python-list



--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen howto?

2009-05-08 Thread norseman

Carl Banks wrote:

On May 7, 2:58 pm, norseman norse...@hughes.net wrote:

If you don't like a lot of typing that obscures the process,
take a look at os.Popen2  Pg.39 or so in Lib.pdf for 2.5.2
In this case - the popen3 is probably your best bet.

I took a test run on subprocess a few months ago. My review:
excessive typing to accomplish the simple.



Hmm, I won't argue that it can be excessive typing, but I think its
purpose isn't to accomplish the simple but to hit all the corners of
subprocesses.

What if you want os.system without the shell?  What if you want popen
with the shell?  On Windows what if I want a subprocess without a
console from a Python program in a console.  Stuff like that.


Shell?  You lost me.
Why would you even use Windows?  Just kidding.
  Build the TSR and Start from Python or the bootup autostart
  and use it from Python or whatever.
Those in Unix, put the   after the program name in the starter
  to get it to background. Be sure to use P_NOWAIT in the starter.


I still use os.system for QD stuff, but now use subprocess for
everything else since I prefer thorough to short and sweet.


As long as speed (volume vs time) isn't a problem - OK.




Carl Banks
--
http://mail.python.org/mailman/listinfo/python-list


==

1)  That one has access to and can make for ones self choices is
  freedom.
2)  As far as forcing a 'do all' into use is concerned,
  especially at the expense of removing others choices
  (Python notes say subprocess is supplanting popen et al),
  well... that is dictitorial, anti-freedom.
3)  Consider this: a do all is as useful as driving a Ferrari on
  a Jeep Jamboree cross crountry race. Or chasing a Ferrari
  through paved mountain roads with a Jeep.  The SUV is
  supposed to be a do all. Would you actually expect it to
  win either race?  After all, they are all cars.
Use/create the proper tool for the job. I do not know any good mechanics 
that have a monkey-wrench (a crescent) in their tool box.  And all of 
them will tell you that the guy who picks one up and starts toward the 
car may be upset with the driver but he is not a mechanic.

(Wrong tool of choice. Besides, should not have been available. :)
(Use the long handle 1/2 socket drive ratchet. Weight, length and 
balance are just right. ;)


Everybody - Read #1 one more time and make it (speak out loud) my way.
If the client is happy, who cares how it was done?  But don't force 
others   (Python admins taking notes?)


Steve

--
http://mail.python.org/mailman/listinfo/python-list


subprocess.Popen howto?

2009-05-07 Thread OJOHANS
Hi,
 
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.
 
Let me include a simple code that simulates the calculating process:
/* This code simulates a big iterative calculation */
#include stdio.h
#include math.h
 
int main()
{
 float val[2] = { M_PI, M_E };
 int i;
 
 for ( i = 0; i  2 i++) {
  sleep( 15 );   /* It's a hard calculation. It take 15 seconds */
  printf(Value: %5.6f\n, val[i] );
  fflush( stdout );
 }
 return 0;
}
 
let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code)
 
In C I have this code which starts the mycalc process and handles the output 
from it:
 
#include stdio.h
#include assert.h
#define BUF_SIZE 256
 
int main()
{
 FILE *pip;
 char line[BUF_SIZE];
 
 pip = popen(mycalc, r);
 assert( pip != NULL );
 
 while ( fgets( line, BUF_SIZE, pip )) {
  printf( Hello; I got: %s \n, line );
  fflush( stdout );
 }
 pclose( pip );
 return 0;
}

How can I make such while-loop in Python? I assume I should use 
subprocess.Popen(), but I can't figure out how?
 
-Øystein
 
 


---
The information contained in this message may be CONFIDENTIAL and is
intended for the addressee only. Any unauthorised use, dissemination of the
information or copying of this message is prohibited. If you are not the
addressee, please notify the sender immediately by return e-mail and delete
this message.
Thank you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen howto?

2009-05-07 Thread norseman

Øystein Johansen (OJOHANS) wrote:

Hi,
 
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.
 
Let me include a simple code that simulates the calculating process:

/* This code simulates a big iterative calculation */
#include stdio.h
#include math.h
 
int main()

{
 float val[2] = { M_PI, M_E };
 int i;
 
 for ( i = 0; i  2 i++) {

  sleep( 15 );   /* It's a hard calculation. It take 15 seconds */
  printf(Value: %5.6f\n, val[i] );
  fflush( stdout );
 }
 return 0;
}
 
let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code)
 
In C I have this code which starts the mycalc process and handles the 
output from it:
 
#include stdio.h

#include assert.h
#define BUF_SIZE 256
 
int main()

{
 FILE *pip;
 char line[BUF_SIZE];
 
 pip = popen(mycalc, r);

 assert( pip != NULL );
 
 while ( fgets( line, BUF_SIZE, pip )) {

  printf( Hello; I got: %s \n, line );
  fflush( stdout );
 }
 pclose( pip );
 return 0;
}
How can I make such while-loop in Python? I assume I should use 
subprocess.Popen(), but I can't figure out how?
 
-Øystein
 
 


---
The information contained in this message may be CONFIDENTIAL and is
intended for the addressee only. Any unauthorised use, dissemination of the
information or copying of this message is prohibited. If you are not the
addressee, please notify the sender immediately by return e-mail and delete
this message.
Thank you.




--
http://mail.python.org/mailman/listinfo/python-list



If you don't like a lot of typing that obscures the process,
take a look at os.Popen2  Pg.39 or so in Lib.pdf for 2.5.2
In this case - the popen3 is probably your best bet.

I took a test run on subprocess a few months ago. My review:
excessive typing to accomplish the simple.  BlackBox stuff is supposed 
to be short and sweet.


BlackBox was the term applied to drivers and couplers back when.
Back when: one started by writing a subroutine that got used.
   next one got to write a program
   next one got to write an application
   then a BlackBox
   from there one could graduate to OS
That was back when!


to repeat from another of my postings:
---
processPython 2.5.2 (r252:60911, Mar  4 2008, 10:40:55)
[GCC 3.3.6] on linux2
Type help, copyright, credits or license for more information.

 import os
 AbWd = os.spawnlp( os.P_WAIT,abiword,abiword,)

The P_WAIT stops python until the program (abiword in this case) 
completes.  The  at the end are for tokens to be given to the program 
and yes - contrary to manual, the program MUST be there TWICE (complete 
with any path needed).


for windows this works:
(for cut and paste from cmd.exe, see:
  Re: Copy  Paste in a Dos box  from norseman  05/06/2009 4:28PM
)

Python 2.5.1 ... on win32
 import os
 result = os.spawnl( os.P_WAIT, d:\\winmcad\\mcad,)

Runs the program mcad. Returns to python when mcad exits.
---


In your case:  substitute ...your_compiled_c_program,  yourPy.py)
at the obvious places.

In case this isn't clear;
  method 1:  py1.py starts compiled.c which redirects to yourPy.py
  method 2:  py1.py uses os.popen3(compiled.c...)  the two work it out.
In either case the receiver at the end stays open until completion so 
sleep() things are not needed.  (may need to test for EOL and EOT)


HTH

Steve
norse...@hughes.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen howto?

2009-05-07 Thread Chris Rebert
2009/5/7 Øystein Johansen (OJOHANS) ojoh...@statoilhydro.com:
 Hi,

 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.

 Let me include a simple code that simulates the calculating process:
 /* This code simulates a big iterative calculation */
 #include stdio.h
 #include math.h

 int main()
 {
  float val[2] = { M_PI, M_E };
  int i;

  for ( i = 0; i  2 i++) {
   sleep( 15 );   /* It's a hard calculation. It take 15 seconds */
   printf(Value: %5.6f\n, val[i] );
   fflush( stdout );
  }
  return 0;
 }

 let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code)

 In C I have this code which starts the mycalc process and handles the output
 from it:

 #include stdio.h
 #include assert.h
 #define BUF_SIZE 256

 int main()
 {
  FILE *pip;
  char line[BUF_SIZE];

  pip = popen(mycalc, r);
  assert( pip != NULL );

  while ( fgets( line, BUF_SIZE, pip )) {
   printf( Hello; I got: %s \n, line );
   fflush( stdout );
  }
  pclose( pip );
  return 0;
 }
 How can I make such while-loop in Python? I assume I should use
 subprocess.Popen(), but I can't figure out how?

import subprocess
subprocess.call([./mycalc]) # add `bufsize=-1` as argument to have
output be buffered
#process inherits our filehandles and writes directly to them
#.call() only returns once the process has exited

Cheers,
Chris
-- 
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen howto?

2009-05-07 Thread Carl Banks
On May 7, 2:58 pm, norseman norse...@hughes.net wrote:
 If you don't like a lot of typing that obscures the process,
 take a look at os.Popen2  Pg.39 or so in Lib.pdf for 2.5.2
 In this case - the popen3 is probably your best bet.

 I took a test run on subprocess a few months ago. My review:
 excessive typing to accomplish the simple.


Hmm, I won't argue that it can be excessive typing, but I think its
purpose isn't to accomplish the simple but to hit all the corners of
subprocesses.

What if you want os.system without the shell?  What if you want popen
with the shell?  On Windows what if I want a subprocess without a
console from a Python program in a console.  Stuff like that.

I still use os.system for QD stuff, but now use subprocess for
everything else since I prefer thorough to short and sweet.


Carl Banks
--
http://mail.python.org/mailman/listinfo/python-list