Node.js – process.channel 属性
当一个节点进程是通过一个 IPC 通道生成的,process.channel 属性提供了对该 IPC 通道的引用。如果不存在 IPC 通道,那么此属性为未定义。
语法
process.channel
示例 1
创建两个文件 "channel.js" 和 "util.js",并复制以下代码片段。创建文件后,使用命令 "node channels.js" 和 "node util.js" 运行代码。
channel.js
// process.channel Property Demo Example // Importing the process modules const cp = require('child_process'); // Getting reference to the child const process = cp.fork(`${__dirname}/util.js`); // Sending the below message to child process.send({ msg: 'Welcome to Tutorials Point' }); console.log(process.channel)
util.js
// This child will consume the message through channel process.on('message', (m) => { console.log('CHILD got message:', m); process.exit() });
输出
Pipe { buffering: false, pendingHandle: null, onread: [Function], sockets: { got: {}, send: {} } } CHILD got message: { msg: 'Welcome to Tutorials Point' }
示例 2
我们再看一个示例。
// process.channel Property Demo Example // Importing the process modules const process = require('process'); // Checking the process channel if(process.channel) console.log("Process Channel exist") else console.log("Process Channel doesn't exist")
输出
Process Channel doesn't exist
广告