Created
April 3, 2022 12:13
-
-
Save rkaw92/bd585191b8236448d5e0d509d787aa9c to your computer and use it in GitHub Desktop.
Node.js worker_threads with MessageChannel clogging up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { MessageChannel, Worker } = require('worker_threads'); | |
const worker = new Worker('./worker.js'); | |
const benchmarkChannel = new MessageChannel(); | |
worker.postMessage({ port: benchmarkChannel.port2 }, [ benchmarkChannel.port2 ]); | |
(function run() { | |
for (let i = 0; i < 1000000; i += 1) { | |
benchmarkChannel.port1.postMessage('hello world'); | |
} | |
setImmediate(run); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { parentPort } = require('worker_threads'); | |
const ports = []; | |
let messageCount = 0; | |
parentPort.on('message', function({ port }) { | |
ports.push(port); | |
port.on('message', function() { | |
messageCount += 1; | |
}); | |
}); | |
setInterval(() => console.log('total messages: %d', messageCount), 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment