Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NodeJS派生的Python进程-Python的process.send()的替代方案?_Python_Node.js_Fork_Ipc - Fatal编程技术网

NodeJS派生的Python进程-Python的process.send()的替代方案?

NodeJS派生的Python进程-Python的process.send()的替代方案?,python,node.js,fork,ipc,Python,Node.js,Fork,Ipc,我正在用NodeJS生成一个Python脚本,在默认情况下,NodeJS会在这个新进程和父进程之间创建一个IPC 使用NodeJS,要将消息从子级发送到父级,我需要process.send({msg:'toto'}) 如何使用Python实现这一点 好的,我找到了,终于很容易了。它只是关于在正确的文件描述符上写入 在NodeJS端参数上,生成如下脚本: var child = child_process.spawn('python', ['hello.py'], { stdio:[null,

我正在用NodeJS生成一个Python脚本,在默认情况下,NodeJS会在这个新进程和父进程之间创建一个IPC

使用NodeJS,要将消息从子级发送到父级,我需要
process.send({msg:'toto'})

如何使用Python实现这一点


好的,我找到了,终于很容易了。它只是关于在正确的文件描述符上写入

在NodeJS端参数上,生成如下脚本:

var child = child_process.spawn('python', ['hello.py'], {
  stdio:[null, null, null, 'ipc']
});

child.on('message', function(message) {
  console.log('Received message...');
  console.log(message);
});
由于“ipc”通道是第4个参数,因此必须在filedescriptor 3上写入。 在Python方面:

import os

os.write(3, '{"dt" : "This is a test"}\n', "utf8")
完成了。您将在子项上收到消息。on('message'回调)


干杯!

您检查过了吗?我刚检查过,所以它是一个单独的模块?有没有办法使用Python提供的内置功能来尝试与NodeJS IPC交互?不幸的是,我不知道。如果知道我会发布一个答案,而不是作为提示的注释:)从node.js读取Python如何?我在stdin上尝试了
ipc
选项,它保持读取并阻止执行-它有一个send()将数据发送到python程序的stdin在python 3中,必须使用
bytes
函数:
os.write(3,bytes(“{”dt:“这是一个测试“}\n”,“utf8”)