Workers in Node can only communicate via a RPC pattern (same as their web counterparts), which is not a bad thing at all: it enables concurrency, like the
go coroutine pattern used in golang.
However, sometimes you just want a drop-in way to execute a function without blocking the main thread (for instance, regenerating a server's cached data without blocking incoming requests to the previous cached data).
For WebWorkers, Workerize has been providing exactly this. You can execute a function normally without having to change its signature, but it runs in a worker under-the-hood.
I created the
node-inline-worker npm package (Github) to achieve the same thing, leveraging the Worker Thread of Node.js instead of the WebWorker API, while keeping the same interface API as Workerize. With it, making a function truly non-blocking is as simple as changing:
const result = await expensiveFn(1, 2)
const result = await require('node-inline-worker')(expensiveFn)(1, 2)
Note: Worker Threads will not magically solve your performance issues, and have some downfalls. For instance, Node.js documentation's states that:
I suggest that you read the Worker Thread documentation page (especially the Notable differences inside a Worker environment section) to find out if workers are suitable for your use case.
For more information on how to use
node-inline-worker, go read the very straightforward example on the Github repo's page.
Have a good day!
Top comments (0)