[mod_python] popen/popen2 usage?

Conrad Steenberg conrad at hep.caltech.edu
Sun Mar 31 20:40:27 EST 2002


On Sun, 2002-03-31 at 20:18, Tony Burger wrote:
I am calling a C executable from within mod_python using the popen2
function imported from os.  I need to get standard output from the C
function and assign it to a mod_python variable which isn't working. The
C function does a basic (void)fprinf(stdout,"%s\n",myVar). I tested the
C function by writing to stderr instead and that works (logged fprintf
output to the Apache error_log). I just can't get any output. Might
anyone know how mod_python handles this function or what it might be
doing? Thanks.

Check whether the stdout of the forked process doesn't get written to
the browser in this case. You need to write to a different file
descriptor. From man popen:
"      Writing to such  a  stream
       writes to the standard input of the command; the command's
       standard output is the same as that of  the  process  that
       called  popen(),  unless  this  is  altered by the command
       itself."

Unless you wanted the child process' output written to the client
browser??

Otherwise doing a pipe() and then a fork() separately should work, e.g.:

  int my_pipe[2]

  pipe(my_pipe);
  child_pid=fork()

  switch (child_pid)
  {  case -1:
              perror ("Error: unable to fork");
              return 0;
              
     case 0: /* the child */
              close(my_pipe[0]); /* close the read end*/

             /* do an exec/execv etc. here */

     default: /* the parent */
              close(my_pipe[1]); /* close the write end */
  }

You may of course have more than one pipe if you want bi-directional
communication: just close the write end in the child and the read end in
the parent of the seconf pipe.

Cheers!

Conrad
_______________________________________________ Mod_python mailing list
Mod_python at modpython.org
http://www.modpython.org/mailman/listinfo/mod_python




More information about the Mod_python mailing list