I'm using child_process.fork in a NodeJS API server. Here's how I call it:
const { fork } = require('child_process');
const dequeuingChild = fork('dequeue-child.js', null, {stdio: 'inherit', detached: false});
...
// inside some function
dequeuingChild.send({ action: ACTION.DEQUEUE }); // btw is this .send() and counterpart .on('message') sync?
...
I have read from the doc about the detached option which says:
On non-Windows platforms ... the child process will be made the leader of a new process group and session...
my concern is what's included in this process group, session, etc. I mean, does it mean that a non detached child process still shares the resources of the master? Can long running codes running in the non detached child still impact the performance of the master process?
I read somewhere that internally, the C++ part of NodeJS creates threads where it delegates the async operations to, and that part is blocking. Does a non-detached child process share those threads (w/c means they'll block events from master process too)? And is there a difference in memory usage/sharing from detached/attached child process?
I'm asking this because I have in my master process the web server which should receive and enqueue (awaited async) requests as fast as it can, then I delegate the dequeue part, which performs more processing and could run slower than the enqueue part to a pre-forked child process (via .send()
). So I want my web server (receiving) part to run as fast as it can and not let the child process bog down or affect it. The child doesn't have to stay when I exit the master, actually I exit it when the master is exited.
Because I don't understand the impact, I'm torn, should I use the detached
option or not?
Thank you very much!