Search code examples
node.jsmulticore

if my node process creates child processes will those child processes be able to take advantage of other processor cores?


Let's say I have a process that is a "manager" and it spins up "workers":

  child = childProcess.fork("./worker", [], {execArgv: args} );
  child.send(startSignal);

The child processes will be doing the heavy lifting while the manager just ... manages.

Will those child processes be locked into using the processor core that the manager is using or will the OS be free to farm them off to other processor cores if the manager core is too busy?

I realize this may be OS specific. I'm developing on Ubuntu but could deploy it out to any linux (likely CoreOS). For posterity Windows and Mac would be worth discussing too.


Solution

  • Linux answer only, short answer YES.

    Long answer: On checking the Node.js docs (last page):-

    This is a special case of the spawn() functionality for spawning Node processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. See child.send(message, [sendHandle]) for details.

    These child Nodes are still whole new instances of V8. Assume at least 30ms startup and 10mb memory for each new Node. That is, you cannot create many thousands of them.

    Since it's a new V8 instance, the kernel will slice the threads and multitask the job. Linux OS been doing multitasking with multi-core a long time ago (from kernel 2.4). Hence, you want to spawn only as many child processes as the CPU. Use require('os').cpus().length to determine the CPU count.

    Mac is unix (Next?) based, so I'd assume that the kernel behaves the same way.