Skip to content

Instantly share code, notes, and snippets.

@briandesousa
Last active February 16, 2019 06:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save briandesousa/28949fe67a47362b1391bcd136debc0e to your computer and use it in GitHub Desktop.
Demonstrating how to use Node worker_thread module with RxJS observable to stream data from a worker thread to it's parent thread.
const { workerData, parentPort } = require('worker_threads');
parentPort.postMessage(`starting heavy duty work from process ${process.pid} that will take ${workerData}s to complete`);
timeLimit = workerData;
timer = 0;
// simulate a long-running process with updates posted back on a regular interval
do {
setTimeout(
(count) => {
parentPort.postMessage(`heavy duty work in progress...${count + 1}s`);
if (count === timeLimit) {
parentPort.postMessage('done heavy duty work');
}
},
1000 * timer,
timer);
} while (++timer !== timeLimit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment