Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS batch multi processing - child processes in a pool (or multithreading)

NodeJS batch multi threading processing - child processes in a pool.

I know a child process is a process, not a thread. I used wrong semantics, because most people know what your intent is when you speak of "multithreading". So I'll keep it in the title.


Imagine a scenario where you continuously have multiple similar and complex things to do using a single custom function or module. It makes a lot of sense to use all your available cores/threads (e.g. 8/16), which is what child_process.fork() is for.

Ideally, you are going to want a number of simultaneous workers and send/callback messages to/from one controller.

node-cpool, fork-pool, child-pool are some modules that do exactly this, but they seem old/unmaintained/impopular.

There are a ton of similar-ish modules, but these seem the most relevant. What they all have in common is a couple of commits, hardly starred, hardly forked, and abandoned.

What is usually the case when I can't find something for a task that seems like something that makes sense in every way, is that there is an even better way that I am missing. Hence my question.

How do I have a managed, queued, multithreaded pool of parallel fork()s for my custom module that does some CPU intensive work?

Multithreaded modules like TAGG and webworker-threads are not the same because they don't support full modules (with binary compiled components).


PS

I am now using fork-pool which seems to do exactly what I want, with some quirks, but I can't believe that such an unknown and impopular module would be the only viable option here.

like image 770
Redsandro Avatar asked Sep 18 '14 11:09

Redsandro


People also ask

Is Nodejs multithreaded?

You can achieve multithreading by generating multiple nodes or Node. js V8 engines which in isolation are single-threaded. It is still correct to say Node. js is not multi-threaded.

Is node js single threaded or multi-threaded?

Node JS applications uses “Single Threaded Event Loop Model” architecture to handle multiple concurrent clients. There are many web application technologies like JSP, Spring MVC, ASP.NET, HTML, Ajax, jQuery etc.

How does node js handle child processes?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.

How does node js handle child threads?

Node. js is a single-threaded language and uses the multiple threads in the background for certain tasks as I/O calls but it does not expose child threads to the developer. But node. js gives us ways to work around if we really need to do some work parallelly to our main single thread process.


1 Answers

I would suggest using something like Redis as your Queue. Here's a tutorial of creating a message bus in Node with Redis and Kue. This will scale pretty well and allow you to have multiple processes, threads, or even machines producing and consuming items to/from the queue.

like image 199
Jessie A. Morris Avatar answered Oct 16 '22 04:10

Jessie A. Morris