使用boost::process 0.5。Can';t为python.exe重定向stdio 我在Windows上使用了一些C++,允许我启动Python .EXE(2.7),并使用STDIN、STDUT和STDRR与之交互。我使用的是Visual Studio 2015、Boost 1.59和Boost Process 0.5

使用boost::process 0.5。Can';t为python.exe重定向stdio 我在Windows上使用了一些C++,允许我启动Python .EXE(2.7),并使用STDIN、STDUT和STDRR与之交互。我使用的是Visual Studio 2015、Boost 1.59和Boost Process 0.5,python,c++,windows,stdout,boost-process,Python,C++,Windows,Stdout,Boost Process,我已经成功地启动了python.exe,通过设置命令行来执行一些操作,例如“python-c”打印“helloworld”,stdout捕获“helloworld” 这是代码: #include <boost/process.hpp> #include <boost/iostreams/device/file_descriptor.hpp> #include <boost/iostreams/stream.hpp> #include <boost/ios

我已经成功地启动了python.exe,通过设置命令行来执行一些操作,例如“python-c”打印“helloworld”,stdout捕获“helloworld”

这是代码:

#include <boost/process.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <iostream>
#include <fstream>

namespace bp = boost::process;
namespace io = boost::iostreams;
using namespace bp;
using namespace bp::initializers;

bp::pipe create_async_pipe(std::string desc)
{
#if defined(BOOST_WINDOWS_API)
    std::string name = "\\\\.\\pipe\\boost_process_async_io\\" + desc;
    HANDLE handle1 = ::CreateNamedPipeA(name.c_str(), PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED, 0, 1, 8192, 8192, 0, NULL);
    HANDLE handle2 = ::CreateFileA(name.c_str(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
    return make_pipe(handle1, handle2);
#elif defined(BOOST_POSIX_API)
    return create_pipe();
#endif
}

int main()
{
    bp::pipe pIn = create_async_pipe("stdout");
    //bp::pipe pOut = create_async_pipe("stdin");
    {
        //io::file_descriptor_sink stdout_sink("C:\\WA\\output.txt");
        io::file_descriptor_sink stdout_sink(pIn.sink, io::close_handle);
        //io::file_descriptor_source stdin_source(pOut.source, io::close_handle);
        bp::child c = execute(
            run_exe("C:\\Python27\\python.exe"),
            set_cmd_line(L"python -c \"print 'hello world'\""),
            bind_stdout(stdout_sink),
            bind_stderr(stdout_sink)//,
            //bind_stdin(stdin_source)
            );
    }

    io::file_descriptor_source stdout_source(pIn.source, io::close_handle);
    //io::file_descriptor_sink stdin_sink(pOut.sink, io::close_handle);

    io::stream<io::file_descriptor_source> is(stdout_source);
    //io::stream<io::file_descriptor_sink> os(stdin_sink);

    //os << "print 'hello world'\r\nexit()\r\n";

    std::string output;
    std::getline(is, output);
    std::cout << output << std::endl;   
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间bp=boost::process;
名称空间io=boost::iostreams;
使用名称空间bp;
使用名称空间bp::初始值设定项;
管道创建异步管道(标准::字符串描述)
{
#如果已定义(BOOST\u WINDOWS\u API)
std::string name=“\\\\.\\pipe\\boost\u process\u async\u io\\”+desc;
HANDLE handle1=::CreateNamedPipeA(name.c_str(),管道访问入站,文件标志重叠,0,1,8192,8192,0,NULL);
HANDLE handle2=::CreateFileA(name.c_str(),GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
回油管(手柄1、手柄2);
#定义的elif(BOOST_POSIX_API)
返回create_pipe();
#恩迪夫
}
int main()
{
管道引脚=创建异步管道(“标准输出”);
//bp::pipe pOut=创建异步管道(“标准管道”);
{
//io::文件描述符接收器标准接收器(“C:\\WA\\output.txt”);
io::文件\描述符\接收器标准接收器\接收器(pIn.sink,io::关闭\句柄);
//io::文件描述符源标准源(pOut.source,io::关闭句柄);
bp::child c=execute(
运行_exe(“C:\\Python27\\python.exe”),
设置命令行(L“python-c”打印“hello world”\”),
绑定标准输出(标准输出接收器),
绑定标准接收器(标准接收器)/,
//绑定标准数据(标准数据源)
);
}
io::文件\描述符\源标准输出\源(pIn.source,io::关闭\句柄);
//io::文件\描述符\接收器标准\接收器(pOut.sink,io::关闭\句柄);
io::流是(标准输出源);
//io::流操作系统(标准接收器);

//操作系统可能太晚了…但它使用-i和-u选项对我有效: 使用Boost1.69、Python3.7

//create synchronous pipe streams
bp::opstream pyin;
bp::ipstream pyout;
//launch child process with the options for unbuffered std_in and out and interpreter
bp::child c("python -u -i", bp::std_in < pyin, bp::std_out > pyout);

//send py instruccions to python process and recover stdout
pyin << "print('Hello');" << endl;
string pyout_str_hello;
pyout >> pyout_str_hello;

pyin << "print('World');" << endl;
string pyout_str_world;
pyout >> pyout_str_world;

cout << pyout_str_hello << ' ' << pyout_str_world << endl;
//创建同步管道流
bp::opstream-pyin;
bp::ipstream-pyout;
//启动带有无缓冲std_输入输出和解释器选项的子进程
子c(“python-u-i,bp::std_-inpyout”);
//将py指令发送到python进程并恢复stdout
pyin pyout_str__你好;
pyin pyout_str_世界;

也许应用程序中的解释器可以更好地为您提供服务,并使用模块运行REPL。虽然这不能回答我的问题,但我将探索这条路线。我不希望嵌入Python,因为Python配置可能会根据安装在哪台计算机上而改变。我必须针对特定的Python版本进行编译。请尝试使用“-i”命令行选项强制Python进入交互模式。您可以在终端/控制台中输入语句吗?同样的问题。Python不会继续运行。我希望它会继续运行,直到我发出“exit()”,但在执行第一个命令后停止。
pyChild.waitForPrompt();             // Waits for >>>, >>?, ..., etc.
pyChild.write("print 'hello world'); // >>> print 'hello world'
std::cout << pyChild.readLine();     // hello world
//create synchronous pipe streams
bp::opstream pyin;
bp::ipstream pyout;
//launch child process with the options for unbuffered std_in and out and interpreter
bp::child c("python -u -i", bp::std_in < pyin, bp::std_out > pyout);

//send py instruccions to python process and recover stdout
pyin << "print('Hello');" << endl;
string pyout_str_hello;
pyout >> pyout_str_hello;

pyin << "print('World');" << endl;
string pyout_str_world;
pyout >> pyout_str_world;

cout << pyout_str_hello << ' ' << pyout_str_world << endl;