📜  Node.js process.send() 方法

📅  最后修改于: 2022-05-13 01:56:32.146000             🧑  作者: Mango

Node.js process.send() 方法

process.send() 方法是进程模块的内置应用程序编程接口,子进程使用它与父进程进行通信。此方法不适用于根进程,因为它没有任何父进程。

句法:

process.send(message, [sendHandle])

参数:此方法接受以下参数:

  • message:必须发送的消息。
  • sendHandle:一个 Socket 或 Server 对象。它是一个可选参数。

返回值:布尔值。如果消息发送成功,则返回 true,否则返回 false。

示例 1:首先,在 Parent.js 中,我们生成子进程。然后开始监听子进程。在 Child.js 中,我们在Child.js中获取消息。然后我们检查 send 方法是否可用,然后使用process.send() 向父级发送消息。

Parent.js
// Require fork method from child_process 
// to spawn child process
const fork = require('child_process').fork;
  
// Child process file
const child_file = 'Child.js';
  
// Spawn child process
const child = fork(child_file);
  
// Start listening to the child process
child.on('message', message => {
  
    // Message from the child process
    console.log('Message from child:', message);
});


Child.js
console.log('In Child.js')
  
// If the send method is available
if(process.send) {
  
    // Send Hello
    process.send("Hello, this is child process.");
}


Parent.js
// Require fork method from child_process 
// to spawn child process
const fork = require('child_process').fork;
  
// Child process file
const child_file = 'Child.js';
  
// Spawn child process
const child = fork(child_file);
  
// Start listening to the child process
child.on('message', message => {
  
    // Message from the child process
    console.log('Message from child:', message);
});


Child.js
console.log('In Child.js')
  
// If the send method is available
if(process.send) {
  
    // Send Hello
    process.send("Hello, this is child process.");
  
    // Send multiple messages
    setTimeout((function() {
        return process.send("This was send after 1 second.");
    }), 1000);
  
    setTimeout((function() {
        return process.send("This was send after 2 seconds.");
    }), 2000);
  
    setTimeout((function() {
        return process.send("This was send after 3 seconds.");
    }), 3000); 
}


Child.js

console.log('In Child.js')
  
// If the send method is available
if(process.send) {
  
    // Send Hello
    process.send("Hello, this is child process.");
}

跑步 Parent.js文件使用以下命令:

node Parent.js

输出:

In Child.js
Message from child: Hello, this is child process.

示例 2:来自子进程的多条消息。

父.js

// Require fork method from child_process 
// to spawn child process
const fork = require('child_process').fork;
  
// Child process file
const child_file = 'Child.js';
  
// Spawn child process
const child = fork(child_file);
  
// Start listening to the child process
child.on('message', message => {
  
    // Message from the child process
    console.log('Message from child:', message);
});

Child.js

console.log('In Child.js')
  
// If the send method is available
if(process.send) {
  
    // Send Hello
    process.send("Hello, this is child process.");
  
    // Send multiple messages
    setTimeout((function() {
        return process.send("This was send after 1 second.");
    }), 1000);
  
    setTimeout((function() {
        return process.send("This was send after 2 seconds.");
    }), 2000);
  
    setTimeout((function() {
        return process.send("This was send after 3 seconds.");
    }), 3000); 
}

跑步 Parent.js文件使用以下命令:

node Parent.js

输出:

In Child.js
Message from child: Hello, this is child process.
Message from child: This was sent after 1 second.
Message from child: This was sent after 2 seconds.
Message from child: This was sent after 3 seconds.

参考: https://nodejs.org/api/process.html#process_process_send_message_sendhandle_options_callback