Reputation: 419
How I can run node server in multiple threads. Let me start with my scenario first. I have 2 file one is server.js and other is process.js now my requirement is that how I can run process.js in other shared thread like a cluster. note:- cluster will not work in my scenario. I want something else the same as the cluster So my question is that will child_process.folk or spawn is the same as cluster ??? Or how I can achieve multiple threading in node without the use of the cluster. Moreover, I walk through child process document but it hasn't clear my doubt. Excuse for my silly question
Upvotes: 3
Views: 4951
Reputation: 5148
If the nature of work handled by process.js is CPU intensive you can use Worker threads API. As you don't want to use a cluster module, worker threads can be useful. In fact, recommended way of handling CPU intensive work.
Cluster module use child_process.fork
to create the child process.
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. read here more https://nodejs.org/api/cluster.html#cluster_how_it_works.
What makes Worker Threads special:
ArrayBuffers
to transfer memory from one thread to another.SharedArrayBuffer
that will be accessible from either thread. It lets you share memory between threads (limited to binary data).Atomics available, it lets you do some processes concurrently, more efficiently and allows you to implement conditions variables in JavaScript.
MessagePort
, used for communicating between different threads. It can be used to transfer structured data, memory regions and other MessagePorts between different Workers.
MessageChannel
represents an asynchronous, two-way communications channel used for communicating between different threads.WorkerData
is used to pass startup data. An arbitrary JavaScript value that contains a clone of the data passed to this thread’s Worker constructor. The data is cloned as if using postMessage()
An Example of worker threads
Main.js(server.js in your case)
const { Worker } = require('worker_threads')
function runWorker() {
const worker = new Worker('./worker.js',{workerData:{i:0}});
worker.on('message',function(data){
console.log(data);
});
worker.on('error', function(error){
console.log(error)
});
worker.on('exit', (code) => {
if (code !== 0)
console.log(new Error(`Worker stopped with exit code ${code}`));
})
}
runWorker()
worker.js (process.js in your case)
const { workerData, parentPort } = require('worker_threads')
/* You can do any heavy stuff here, in a synchronous way
without blocking the "main thread"*/
console.log(workerData)
let i=workerData.i
for(;i<500;i++){
parentPort.postMessage({I:i})
}
Resources that can be useful to get started with:
https://nodesource.com/blog/worker-threads-nodejs/
https://medium.com/@Trott/using-worker-threads-in-node-js-80494136dbb6
https://medium.com/nodejsmadeeasy/workers-threads-in-node-js-part2-ec45ac084963
Upvotes: 3