Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js child_process.fork() to run on different CPU cores

I have an application that runs long-executing processes. To make it faster, I do simple sharding of data and want to run them in parallel, simply by .fork() 2 instances of same application.

I'm having 2 Cores machine there and want to make sure that 2 Cores are utilized and first instance is running on first core, second on second core.

I know about cluster module, but it seems not relevant in this case, since I don't need HTTP services running and load-balancing between them. Just workers (mean, they dont need to communicate with each other, send messages or whatever - they just do HTTP requests and store data to database).

Is there possible at all to control which CPU core would node.js process take? How to monitor that on Mac/Linux?

like image 306
Alexander Beletsky Avatar asked Aug 07 '13 14:08

Alexander Beletsky


People also ask

How do I run multiple cores in NodeJS?

You may run your node. js application on multiple cores by using the cluster module on combination with os module which may be used to detect how many CPUs you have. Show activity on this post. I'm using Node worker to run processes in a simple way from my main process.

CAN NodeJS use multiple cores?

NodeJS uses Javascript to develop server side applications and shares the same behavior. It runs on one CPU core regardless of how many CPU cores you have in your machine or a virtual machine in the cloud.

How does NodeJS support multi processor platforms?

Since Node. js is by default a single thread application, it will run on a single processor core and will not take full advantage of multiple core resources. However, Node. js provides support for deployment on multiple-core systems, to take greater advantage of the hardware.


1 Answers

Cluster module is exactly what you need : http://nodejs.org/api/cluster.html

like image 121
jsebfranck Avatar answered Oct 30 '22 20:10

jsebfranck