how libuv threads in nodejs utilize multi core cpu
Asked Answered
H

2

7

I am not able to find out whether libuv in node.js uses multi core cpus or it runs all of its threads in on single core only using time slicing? As node.js is single threaded but libuv has its own thread pool so does it use all the cores of multi core cpu?

Hannover answered 20/9, 2015 at 12:0 Comment(0)
W
7

It does leverage multicores via the threadpool. e.g., on Linux, the underlying pthreads will uses multiple cores for multiple threads.

If you run the following code, you will notice 4 (default threadpool size) cores will peg at 100% as file system IO are running with the threadpool.

var util = require('util');
var fs = require('fs');

for (var i = 0; i < 300000; i++) {
    (function(id) {
        fs.readdir('.', function() {
           console.log(util.format('readdir %d finished.', id));
       });
    })(i);
}
Waziristan answered 21/11, 2015 at 22:41 Comment(0)
S
-1

A Node.js server can use all cores through the Cluster module. Otherwise it will not, unless you build your application as multiple processes which is a common pattern.

Supervisory answered 20/9, 2015 at 12:3 Comment(1)
yes I understand that, but at deeper level of node there is libuv threadpool which uses POSIX threads which theoratically should use miltiple cores. So my question is that this threadpool in libuv uses multi cores or not.Hannover

© 2022 - 2024 — McMap. All rights reserved.