cgi integration question 2

Ken ken at hotmail.com
Sun Oct 20 06:02:43 EDT 2002


"Ken" <ken at hotmail.com> wrote in message
news:ao6gd8$jrd5h$1 at ID-49758.news.dfncis.de...
>
> "Gerhard Häring" <gerhard.haering at opus-gmbh.net> wrote in message
> news:slrnaqdgb7.164.gerhard.haering at haering.opus-gmbh.net...
> > Ken <ken at hotmail.com> [2002-10-11 12:03 GMT]:
> > > Hi there, I have a question on integration with python and C++.
> > >
> > > I have 2 sets of code. Python is used to display interface and C++ is
> used
> > > for backend execution. The executed result is saved to file so the
> python can
> > > read it and display back on the screen.
> > >
> > > When I execute the python  program:
> > >
> >
> > > 1. How do I make the python to initiate execution of the C++ program
and
> pass
> > >    over values/arguments?
> > >
> > > 2. How does python know when the C++ program finished execution so it
> can
> > >    open the result file to read from?
> >
> > No need for intermediary files, just use os.popen*.
> >
> > Here's a simple example:
> >
> > >>> import os
> > >>> outf, inf = os.popen2("cat")
> > >>> print >>outf, "hi"
> > >>> outf.close()
> > >>> print inf.read()
> > hi
> >
> > outf is a stream to write to your child process (in this example, the
Unix
> cat
> > utility, which will just duplicate on stdout what it gets on stdin).
After
> > writing the "parameters" to cat's stdin, I close it (don't ask me why
> that's
> > necessary, but this is the way it works :-D). Then, I read the whole
> output of
> > cat and print it.
> >
> > HTH,
> >
> > -- Gerhard
>
> Hi, can you explain what is "inf" and "popen2" in:
> outf, inf = os.popen2("cat")   ?
>
> Is "cat" suppose to be a program name for me to call?
>
> So.... if I get the idea right, my program should look like this:
> import os
> outf, inf = os.popen2("cat")
> print >>outf, "hi"
> outf.close()
> print inf.read()  # does this mean  read from file?
> =====
>
> and the backend c++ program look somethingl like this:
> executable filename: cat
>
> #include <iostream>
> using namespace std;
>
> int main (int argc, char** argv) {
>       cout << "value from argv"<< endl;
>       return 0;
> }
>
> Still don't get how values are passed to the backend though....can you
> please explain.... : (
>
> Thanks
>
=============================================

Hello, I have this problem which I don't know what it means or how to fix,
can someone help me?

Traceback (most recent call last): File "chessboard.cgi", line 143, in ?
outf.close() IOError: [Errno 32] Broken pipe

Python frontend program:
  lines = []
  outf, inf = os.popen2("knight")
  print >>outf, size #size
  print >>outf, startX #start X position
  print >>outf, startY #start Y position
  outf.close()  #<<========= Refer to this line 143
  time.sleep(1)
  maxSize = string.atoi(size) * string.atoi(size)
  while len(lines)< maxSize:
    lines = inf.readlines() #Output produce by back end program ["34", "35",
...etc]
    if len(lines)==maxSize:
      inf.close()
      break
    time.sleep(1)

===============
C++ backend program:

int main() {
  //size of board: Test uses size 3
  string size, startX, startY;
  cin>>size;
  cin>>startX;
  cin>>startY;

  vector<string> finalResult;
  finalResult.push_back("12");
  finalResult.push_back("13");
  finalResult.push_back("33");
  finalResult.push_back("31");
  finalResult.push_back("32");
  finalResult.push_back("22");
  finalResult.push_back("21");
  finalResult.push_back("23");
  finalResult.push_back("11");

  for (unsigned int i=0; i<finalResult.size();i++){
    cout<<finalResult[i]<<endl;
    }
  }







More information about the Python-list mailing list