Last active
February 16, 2019 06:45
-
-
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.
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 { 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