pythonshell和多处理不打印

2024-09-26 22:07:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个问题。 我使用nodejs的pythonshellnpm包,该包允许向python发送IPC消息,并将python脚本的print语句作为IPC返回

所以首先,我创建了一个完全工作的python脚本,它接受stdin并打印到stdout

然后我实现了pythonshellipc,向python脚本发送一条消息,一切正常

问题始于我在python脚本中创建了一个进程(使用multiprocessing.process),并将活动移植到该脚本中

在这里,我注意到新创建的进程的stdout不是通过pythonshell接收的!但这怎么可能呢

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

例如,关于同一问题,可以在previous post找到可调试代码

请-任何线索都可能有帮助


Tags: 脚本消息标准进程stdinstdoutnodejs语句
1条回答
网友
1楼 · 发布于 2024-09-26 22:07:26

可能和我现在做的一样。你可以用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');
});

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

相关问题 更多 >

    热门问题