Last active
June 20, 2020 06:50
-
-
Save bobby569/370ca2358a24d6243e074d4a416b79e9 to your computer and use it in GitHub Desktop.
Multithreading for WebWorker
This file contains hidden or 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 { 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