Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference with childprocess.fork between cluster.fork

Tags:

node.js

Simple question: What's the difference with child_process.fork between cluster.fork

detail:

  1. can I pass arguments to cluster.fork

  2. can I listen on same port or unixsock for ChildProcess create by child_process.fork

like image 654
guilin 桂林 Avatar asked May 01 '12 03:05

guilin 桂林


People also ask

What is the difference between cluster fork () vs child_process fork () in node JS?

In a single thread, the individual instance of node. js runs specifically and to take advantage of various ecosystems, a cluster of node. js is launched, to distribute the load. With the help of a cluster module, child processes can be created very easily sharing the server ports.

What does cluster fork do?

Cluster-Fork runs a command on compute nodes of your cluster. Often we want to execute parallel jobs consisting of standard UNIX commands. By "parallel" we mean the same command runs on multiple nodes of the cluster.

What is the difference between cluster and Worker_threads packages in node JS?

Clusters of Node. js processes can be used to run multiple instances of Node. js that can distribute workloads among their application threads. When process isolation is not needed, use the worker_threads module instead, which allows running multiple application threads within a single Node.


1 Answers

Read the docs: child_process.fork vs cluster.fork.

The difference between cluster.fork() and child_process.fork() is simply that cluster allows TCP servers to be shared between workers. cluster.fork is implemented on top of child_process.fork.

http://nodejs.org/api/cluster.html


1. can I pass arguments to cluster.fork

Not according to the docs, and:

> var cluster = require('cluster')
undefined
> cluster
{ isWorker: false,
  isMaster: true,
  fork: [Function],
  _startWorker: [Function],
  _getServer: [Function] }
> cluster.fork.length
0

(a function's length is its number of formal parameters). Use message passing instead.

2. can I listen on same port or unixsock for ChildProcess create by child_process.fork

Presumably yes, since cluster.fork is implemented on top of child_process.fork. However, there is a reason that cluster.fork already exists, if you want to listen on the same port.

like image 100
Matt Ball Avatar answered Oct 16 '22 01:10

Matt Ball