python - python-shell和多处理不打印
问题描述
我有个问题。我使用 nodejs 的 python-shell npm 包,它允许将 IPC 消息发送到 python,并将 python 脚本的打印语句作为 IPC 读取。
所以首先我创建了一个完全工作的 python 脚本,它接受标准输入并打印到标准输出。
然后我实现了 python-shell IPC 向 python 脚本发送消息,一切正常。
当我在 python 脚本中创建一个进程(使用 multiprocessing.Process)并将活动移植到那里时,问题就开始了。
在这里,我注意到新创建的进程的标准输出不是通过 python-shell 接收的!但怎么可能呢?
进程标准输出与运行它的脚本不一样吗?
例如,可以在上一篇关于同一问题的帖子中找到可调试代码。
请 - 任何线索可能会有所帮助。
解决方案
可能是我目前正在做的同样的事情。您可以使用 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');
});
如果您还有问题,请告诉我。快乐编码。
推荐阅读
- python - 将分数输入 a/b / c/d 分配给 x = a/b, y = c/d sign= /。Python
- hibernate - 使用 uuid id 生成器的 Grails:不是很随机
- assembly - 在 MIPS 汇编中,如果操作码以 000000 开头,我们如何知道它所指的函数是什么?
- javascript - 使用 JS 循环添加/删除 CSS 类
- ios - 如何在IOS swift中的http post请求中发送带有值键的JSON对象作为参数
- c# - JsonConvert.DeserializeObject 数组
- .net - 在 AWS X-RAY SDK .NET 中动态创建分段
- enums - 如果我们遵循枚举本地化约定,ABP 可以在哪些情况下自动本地化枚举
- spring-boot - 无法显示会话的自定义消息[SPRING_SECURITY_LAST_EXCEPTION]
- python - 当多个用户连接到应用程序时,Web 服务非常慢