Alexander Beletsky
Alexander Beletsky

Reputation: 19841

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?

Upvotes: 7

Views: 3309

Answers (2)

Alexander Mills
Alexander Mills

Reputation: 100476

You want this, which is a specific part of the cluster API:

cluster.setupMaster([settings])

https://nodejs.org/api/cluster.html#cluster_cluster_setupmaster_settings

I also wrote a module to do this, and there are several out there:

https://www.npmjs.com/package/poolio

my module works well, but the API is not that straightforward, so I would recommend using the core module

Upvotes: 0

jsebfranck
jsebfranck

Reputation: 724

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

Upvotes: 3

Related Questions