Skip to content

Instantly share code, notes, and snippets.

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
What would you like to do?
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 ${} 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 {
(count) => {
parentPort.postMessage(`heavy duty work in progress...${count + 1}s`);
if (count === timeLimit) {
parentPort.postMessage('done heavy duty work');
1000 * timer,
} while (++timer !== timeLimit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment