首页 > 解决方案 > python-shell和多处理不打印

问题描述

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

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

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

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

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

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

例如,可以在一篇关于同一问题的帖子中找到可调试代码。

请 - 任何线索可能会有所帮助。

标签: pythonnode.jsipcstdoutpython-multiprocessing

解决方案


可能是我目前正在做的同样的事情。您可以使用 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');
});

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


推荐阅读