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

更新於: 29-10-2021

164 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.