【问题标题】:python-shell and multiprocessing not printingpython-shell和多处理不打印
【发布时间】:2020-03-30 22:16:35
【问题描述】:

我有问题。 我使用 nodejs 的 python-shell npm 包,它允许向 python 发送 IPC 消息,并将 python 脚本的打印语句作为 IPC 读取。

所以首先我创建了一个完全工作的 python 脚本,它接受标准输入并打印到标准输出。

然后我实现了python-shell IPC向python脚本发送消息,一切正常。

当我在 python 脚本中创建一个进程(使用 multiprocessing.Process)并将活动移植到那里时,问题就开始了。

在这里我注意到新创建的进程的标准输出不是通过 python-shell 接收的!但怎么可能呢?

进程标准输出与运行它的脚本不一样吗?

可在previous post 找到有关同一问题的示例可调试代码。

请 - 任何线索都可以提供帮助。

【问题讨论】:

    标签: python node.js ipc stdout python-multiprocessing


    【解决方案1】:

    可能与我目前正在做的事情相同。您可以使用 PythonShell。看个例子你就明白了。

    var myPythonScriptPath = 'script.py';
    
    // Use python shell
    var PythonShell = require('python-shell');
    var pyshell = new PythonShell(myPythonScriptPath);
    
    pyshell.on('message', function (message) {
    // received a message sent from the Python script (a simple "print" statement)
    console.log(message);
    });
    
    // end the input stream and allow the process to exit
    pyshell.end(function (err) {
    if (err){
        throw err;
    };
    
    console.log('finished');
    });
    

    如果您仍有问题,请告诉我。快乐编码。

    【讨论】:

    • 我在做同样的事情
    • 那是什么问题?
    • 来自 multiprocessing.Process 进程的 python prints() 不会被 pyshell.on() 捕获
    • 我也在做多处理。你使用 child_process 吗?
    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多