An open API service indexing awesome lists of open source software.

https://github.com/pkief/nodejs-worker-threads

Demo for NodeJS WorkerThreads
https://github.com/pkief/nodejs-worker-threads

nodejs typescript worker-threads

Last synced: 4 months ago
JSON representation

Demo for NodeJS WorkerThreads

Awesome Lists containing this project

README

        

# NodeJS Worker Threads

This example demonstrates how NodeJS can use multiple threads for long running tasks. Use cases can be long running calculations, compressions or encryptions that should not block the main thread.

## Requirements

- NodeJS 10.x or NodeJS 12

## Installation

- `npm install`
- `npm run build`
- `npm start`

## Additional Explanation

During the execution the index script calls the worker script and prints each second a log message to the console. In the meanwhile the worker is doing some simple calculations that cost some time. When the worker is done, the interval of log messages is stopped and the result of the sum is printed out.

```bash
[1s] Worker is working...
[2s] Worker is working...
[3s] Worker is working...
[4s] Worker is working...
[5s] Worker is working...
[6s] Worker is working...
[7s] Worker is working...
[8s] Worker is working...
[9s] Worker is working...
[10s] Worker is working...
[11s] Worker is working...
[12s] Worker is working...
[13s] Worker is working...
[14s] Worker is working...
[15s] Worker is working...
[16s] Worker is working...
[17s] Worker is working...
{ result: 5000000000, status: 'Done' }
```