📜  Node.js process.channel 属性

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

Node.js process.channel 属性

process.channel是 process 模块中 Process 类的内置应用程序编程接口,用于获取对 IPC 通道的引用。如果不存在 IPC 通道,则该属性未定义。

句法:

const process.channel

参数:此 api 不接受任何参数作为参数。

返回值:该接口返回对IPC通道的引用。如果不存在 IPC 通道,则该属性未定义。

示例 1:

index.js
// Node.js program to demonstrate the  
// Process.channel Property
  
// Importing process modules
const cp = require('child_process');
  
// Getting child process reference 
const process = cp.fork(`${__dirname}/sub.js`);
  
// Causes the child to print: 
// CHILD got message: { hello: 'world' }
process.send({ hello: 'world' });
  
console.log(process.channel)


sub.js
process.on('message', (m) => {
  console.log('CHILD got message:', m);
  
  process.exit()
});


index.js
// Node.js program to demonstrate the  
// Process.channel Property
  
// Importing process modules
const process = require('process');
  
// Getting process channel
if(process.channel) 
   console.log("Process Channel exist")
else
   console.log("Process Channel doesn't exist")


子.js

process.on('message', (m) => {
  console.log('CHILD got message:', m);
  
  process.exit()
});

使用以下命令运行index.js文件:

node index.js

输出:

Control {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false
}
CHILD got message: { hello: 'world' }

示例 2:

index.js

// Node.js program to demonstrate the  
// Process.channel Property
  
// Importing process modules
const process = require('process');
  
// Getting process channel
if(process.channel) 
   console.log("Process Channel exist")
else
   console.log("Process Channel doesn't exist")

使用以下命令运行index.js文件:

输出:

Process Channel doesn't exist

参考: https://nodejs.org/dist/latest-v16.x/docs/api/process.html#process_process_channel