从嵌入式Python异步将stdout / stdin重定向到C ++? [英] Asynchronously redirect stdout/stdin from embedded python to c++?

查看:88
本文介绍了从嵌入式Python异步将stdout / stdin重定向到C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实质上是在尝试为嵌入式python脚本编写一个带有输入和输出的控制台界面。按照说明在这里,我能够捕获stdout:

I am essentially trying to write a console interface with input and output for an embedded python script. Following the instructions here, I was able to capture stdout:

Py_Initialize();
PyRun_SimpleString("\
class StdoutCatcher:\n\
    def __init__(self):\n\
        self.data = ''\n\
    def write(self, stuff):\n\
        self.data = self.data + stuff\n\
import sys\n\
sys.stdout = StdoutCatcher()");

PyRun_SimpleString("some script");

PyObject *sysmodule;
PyObject *pystdout;
PyObject *pystdoutdata;    
char *string;
sysmodule = PyImport_ImportModule("sys");
pystdout = PyObject_GetAttrString(sysmodule, "stdout");
pystdoutdata = PyObject_GetAttrString(pystdout, "data");    
stdoutstring = PyString_AsString(pystdoutdata);

Py_Finalize();

此问题是我只在脚本后之后收到stdout已完成运行,而理想情况下,对于控制台,stdoutstring将随着python脚本更新而更新。有办法做到吗?

The problem with this is that I only recieve the stdout after the script has finished running, whereas ideally for a console the stdoutstring would update as the python script updates it. Is there a way to do this?

此外,我将如何捕获标准输入?

Also, how would I go about capturing stdin?

如果帮助,我正在使用接受Objective-C的编译器。我也有可用的增强库。

If it helps, I am working with a compiler that accepts Objective-C. I also have the boost libraries available.

我知道问题的标准部分。对于后代来说,这可行:

I've figured out the stdout part of the question. For posterity, this works:

static PyObject*
redirection_stdoutredirect(PyObject *self, PyObject *args)
{
    const char *string;
    if(!PyArg_ParseTuple(args, "s", &string))
        return NULL;
    //pass string onto somewhere
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef RedirectionMethods[] = {
    {"stdoutredirect", redirection_stdoutredirect, METH_VARARGS,
        "stdout redirection helper"},
    {NULL, NULL, 0, NULL}
};

//in main...
    Py_Initialize();
    Py_InitModule("redirection", RedirectionMethods);
    PyRun_SimpleString("\
import redirection\n\
import sys\n\
class StdoutCatcher:\n\
    def write(self, stuff):\n\
        redirection.stdoutredirect(stuff)\n\
sys.stdout = StdoutCatcher()");

    PyRun_SimpleString("some script");

    Py_Finalize();

仍然无法使用stdin ...

Still having trouble with stdin...

推荐答案

到目前为止,我发现最简单的方法如下:

Easiest way I found so far to do this is as follows:

PyObject *sys = PyImport_ImportModule("sys");
PyObject* io_stdout = PyFile_FromFile(stdout, "stdout", "a", nullptr);
PyObject_SetAttrString(sys, "stdout", io_stdout);
PyObject* io_stderr = PyFile_FromFile(stderr, "stderr", "a", nullptr);
PyObject_SetAttrString(sys, "stderr", io_stderr);
PyObject* io_stdin = PyFile_FromFile(stdin, "stdin", "r", nullptr);
PyObject_SetAttrString(sys, "stdin", io_stdin);

您可以使用以下命令对其进行测试:

you can test it with:

# for test
PyRun_SimpleString("print sys.stdin.readline()");

这篇关于从嵌入式Python异步将stdout / stdin重定向到C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆