Skip to content

Instantly share code, notes, and snippets.

@bobby569
Last active June 20, 2020 06:50
Show Gist options
  • Select an option

  • Save bobby569/370ca2358a24d6243e074d4a416b79e9 to your computer and use it in GitHub Desktop.

Select an option

Save bobby569/370ca2358a24d6243e074d4a416b79e9 to your computer and use it in GitHub Desktop.
Multithreading for WebWorker
const { Worker } = require('webworker-threads');
const app = require('express')();
app.get('/', (req, res) => {
const worker = new Worker(function() {
this.onmessage = function() {
let cnt = 0;
while (cnt < 1e9) {
cnt++;
}
postMessage(cnt);
};
});
worker.onmessage = function(myCnt) {
const { data } = myCnt;
console.log(data);
res.send('' + data);
};
worker.postMessage();
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment