How do you start a node process, targetting a specific CPU core?
I've seen node cluster, but I'm interested in starting two different processes, on different cores.
I was assuming there was a way of doing this when starting node from the command line, i.e:
node myapp.js
I'd be interested to know how to do this in both windows and linux, if there is a difference.
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.
js being single-threaded. The single-threaded implementation makes Node a bad choice for CPU-intensive programs. When a time-consuming task is running in the program it blocks the event loop from moving forward for a longer period.
Introduction. A single instance of a Node. js application runs on only one thread and, therefore, doesn't take full advantage of multi-core systems. There will be times when you may want to launch a cluster of Node.
libuv: libuv is a C library originally written for Node. js to abstract non-blocking I/O operations. Event-driven asynchronous I/O model is integrated. It allows the CPU and other resources to be used simultaneously while still performing I/O operations, thereby resulting in efficient use of resources and network.
In general, the scheduler will do a pretty good job of this without any help. In the wild, I've only seen one situation where this mattered....
We were deploying a node service on an 8-core box and during load testing we made the strangest observation... the service actually performed better with 7 workers than with 8. A bit of debugging later and we figured out that all network interrupts were being handled by core 0. I played with using 15 workers so that core0 would have a 1/2 share of the load compared to the other cores. Ultimately, I think we ended up going with 7 workers because it was simpler and more predictable and the complexity just wasn't worth it to get ~7% more theoretic throughput.
That being said, to set CPU affinity on linux:
$ taskset -pc 3089
pid 3089's current affinity list: 0,1
$ taskset -p 3089
pid 3089's current affinity mask: 3 # core 0 = 0x1, core 1 = 0x2
$ taskset -pc 1,2,3 3089
pid 3089's current affinity list: 0,1
pid 3089's new affinity list: 1
On linux you can use taskset
to run node with a given CPU affinity. See this post for information on using the start
command in Windows to do the same.
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