gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:05 27 4
gpt4 key购买 nike

Python process forked by NodeJS - Alternative to process.send() for Python?

我遵循了上面的解决方案,但似乎不起作用(子 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);
});

# !/usr/bin/python3
import os

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

我知道可能会出现什么问题。请帮忙。

最佳答案

我认为第四个参数需要是“ipc”而不是“pipe”才能启用这种消息传递方式。

'ipc' - Create an IPC channel for passing messages/file descriptors between parent and child. A ChildProcess may have at most one IPC stdio file descriptor. Setting this option enables the subprocess.send() method. If the child writes JSON messages to this file descriptor, the subprocess.on('message') event handler will be triggered in the parent. 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.

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

来自默认设置的 fork 的描述(与其他语言的 spawn 不同):

stdio | See child_process.spawn()'s 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. For instance [0, 1, 2, 'ipc'].

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

关于Python [子]进程向 Node [父]进程发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337245/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com