Poco, child communication hangs (python interpreter is child)

185 views Asked by At

I want communicate with python & lua scripts, but python not working as expected. Python hangs:

Log:

lua << 'hello!',   lua >> 'hello!'
lua << 'hello!',   lua >> 'hello!'
lua << 'hello!',   lua >> 'hello!'
python << 'hello!',   

Main C++ application:

#include <iostream>
#include "Poco/Process.h"
#include "Poco/PipeStream.h"
void test( char const* interpreter, char const* filename )
{
   std::vector<std::string> args { filename };
   Poco::Pipe outPipe;
   Poco::Pipe inPipe;
   Poco::ProcessHandle process_handle = Poco::Process::launch( interpreter, args, &inPipe, &outPipe , nullptr/*errPipe*/ );
   Poco::PipeInputStream output_reader(outPipe);
   Poco::PipeOutputStream input_writer(inPipe);
   for(int repeat_counter=0; repeat_counter<3; ++repeat_counter)
   {
      auto send_str("hello!");
      input_writer << send_str << std::endl;
      std::cout << interpreter << " << '" << send_str << "',   " );
      std::cout.flush();
      std::string receiv_str;
      output_reader >> receiv_str;
      std::cout << interpreter << " >> '" << receiv_str << "'" << std::endl;
   }
}
int main()
{
   test("lua","test.lua");
   test("python","test.py");
   return 0;
}

Lua script:

for i=1,10 do
   print(io.read())
end

Python script:

for i in range(0,10):
   print(raw_input())

In Terminal both scripts works identically.

Solution: For Python must use only sys.stdout.write & flush, Thanks @greatwolf

1

There are 1 answers

0
greatwolf On BEST ANSWER

My guess is that python's print function isn't directly outputting to stdout or there's some funny business happening behind the scenes. Try replacing it with sys.stdout.write instead. Don't forget to import sys first.