Simple question: What's the difference with child_process.fork between cluster.fork
detail:
can I pass arguments to cluster.fork
can I listen on same port or unixsock for ChildProcess create by child_process.fork
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.
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.
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.
Read the docs: child_process.fork
vs cluster.fork
.
The difference between
cluster.fork()
andchild_process.fork()
is simply that cluster allows TCP servers to be shared between workers.cluster.fork
is implemented on top ofchild_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With