简体   繁体   English

Python [子]进程向节点[父]进程发送消息

[英]Python [child] process sending message to Node [parent] process

Python process forked by NodeJS - Alternative to process.send() for Python? NodeJS派生的Python进程-Python的process.send()的替代方案?

I followed the solution above but doesnt seem to work (no messages are being send by the child python code. Here is my code: 我遵循了上面的解决方案,但似乎没有用(子python代码未发送任何消息。这是我的代码:

const spawn = require('child_process').spawn;

var child = spawn('python3', ['child.py'], {
    stdio:[null, null, null, 'pipe']
});

child.on('message', function(message) {
    console.log('Received message...');
    console.log(message);
});

and

# !/usr/bin/python3
import os

os.write(3, str.encode("HELLO"))

I can see what could go wrong. 我可以看到可能出了什么问题。 Please help. 请帮忙。

I think the fourth parameter needs to be 'ipc' not 'pipe' to enable this style of message passing. 我认为第四个参数需要是“ ipc”而不是“ pipe”以启用这种样式的消息传递。

'ipc' - Create an IPC channel for passing messages/file descriptors between parent and child. 'ipc'-创建一个IPC通道,用于在父级和子级之间传递消息/文件描述符。 A ChildProcess may have at most one IPC stdio file descriptor. 一个ChildProcess最多可以具有一个IPC stdio文件描述符。 Setting this option enables the subprocess.send() method. 设置此选项将启用subprocess.send()方法。 If the child writes JSON messages to this file descriptor, the subprocess.on('message') event handler will be triggered in the parent. 如果子级将JSON消息写入此文件描述符,则将在父级中触发subprocess.on('message')事件处理程序。 If the child is a Node.js process, the presence of an IPC channel will enable process.send(), process.disconnect(), process.on('disconnect'), and process.on('message') within the child. 如果子进程是Node.js进程,则IPC通道的存在将启用进程中的process.send(),process.disconnect(),process.on('disconnect')和process.on('message')。儿童。

https://nodejs.org/api/child_process.html#child_process_options_stdio https://nodejs.org/api/child_process.html#child_process_options_stdio

From the description of the fork which sets this up by default (unlike spawn for other languages): 从默认情况下进行设置的fork的描述中(不同于其他语言的spawn):

stdio | stdio | See child_process.spawn()'s stdio. 参见child_process.spawn()的stdio。 When this option is provided, it overrides silent. 提供此选项后,它将覆盖无声。 If the array variant is used, it must contain exactly one item with value 'ipc' or an error will be thrown. 如果使用数组变量,则它必须恰好包含一个值为'ipc'的项目,否则将引发错误。 For instance [0, 1, 2, 'ipc']. 例如[0,1,2,'ipc']。

https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM