Use workers when doing lot of work that can be spawned off in another “thread”.
How do you open parallel work threads today? Possibly using async functions?
Async functions do not block the main thread but allow processing to take place in parallel. Well, almost in parallel - see how event loops work.
Although async functions are non-blocking, the call back functions included therein do block the thread. One of the ways of managing truly async functions is through “workers”.
Using workers is a way of creating a “parallel” thread to process a large body of work, while maintaining the main thread going with its own thing.
You can do this pretty easily -
- Include your business logic that takes considerable CPU/time in another file.
- Call the script using worker
This is how the worker looks..
|
|
And, we can call the worker using any async function.
|
|
There are multiple ways in which you can utilize this method in real-world. For example -
- Split thousands or millions of records into blocks, and call five workers to process data (change data, validate them as per new rules etc.)
- Call file operations to batch process files FTP’d to the server (FTP, really? This is 2019)
- Invoke independent API calls to communicate with external systems for a specific transaction