Skip to content

Instantly share code, notes, and snippets.

@Trott
Last active December 10, 2019 06:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trott/c902a350de3fc8112a2227d02e7de49e to your computer and use it in GitHub Desktop.
Save Trott/c902a350de3fc8112a2227d02e7de49e to your computer and use it in GitHub Desktop.
const { Worker, isMainThread, parentPort } = require('worker_threads');
if (isMainThread) {
// This code is executed in the main thread and not in the worker.
// Create the worker.
const worker = new Worker(__filename);
// Listen for messages from the worker and print them.
worker.on('message', (msg) => { console.log(msg); });
} else {
// This code is executed in the worker and not in the main thread.
// Send a message to the main thread.
parentPort.postMessage('Hello world!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment