Home:ALL Converter>What is the difference between Child_process and Worker Threads?

What is the difference between Child_process and Worker Threads?

Ask Time:2019-05-26T18:28:43         Author:Abdelfattah

Json Formatter

I am trying to understand Threading in NodeJS and how it works.

Currently what i understand:

Cluster: -

  • Built on top of Child_process, but with TCP distributed between clusters.
  • Best for distributing/balancing incoming http requests, while bad for cpu intensive tasks.
  • Works by taking advantage of available cores in cpu, by cloning nodeJS webserver instances on other cores.

Child_process:

  • Make use also of different cores available, but its bad since it costs huge amount of resources to fork a child process since it creates virtual memory.

  • Forked processes could communicate with the master thread through events and vice versa, but there is no communication between forked processes.

Worker threads:

  • Same as child process, but forked processes can communicate with each other using bufferArray

1) Why worker threads is better than child process and when we should use each of them?

2) What would happen if we have 4 cores and clustered/forked nodeJS webserver 4 times(1 process for each core), then we used worker threads (There is no available cores) ?

Author:Abdelfattah,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56312692/what-is-the-difference-between-child-process-and-worker-threads
yy