7

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?

Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86
  • 1
    You might check this topic: http://stackoverflow.com/questions/663958/how-to-control-which-core-a-process-runs-on – moka Aug 07 '13 at 15:03
  • 1
    I don't think you can program a thread to run a CPU core. This will handicap the OS as it cannot context switch and hence multitask. Such decisions are best left to the OS. – user568109 Aug 07 '13 at 15:08
  • http://stackoverflow.com/a/8685968/941764 – jgillich Mar 25 '14 at 21:01
  • Google "cpu affinity". It's not node specific though and different OSes do it differently. Plus, from my own googling it seems that on the Mac doing this may reduce performance because the kernel can no longer intelligently manage cache affinity. – slebetman Aug 15 '14 at 11:59

2 Answers2

3

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

jsebfranck
  • 704
  • 2
  • 9
  • 18
0

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

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817