nodejs中的libuv线程如何利用多核cpu [英] how libuv threads in nodejs utilize multi core cpu

查看:287
本文介绍了nodejs中的libuv线程如何利用多核cpu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法确定node.js中的libuv是使用多核cpus还是仅使用时间切片在单核上运行其所有线程?
由于node.js是单线程的,但libuv有自己的线程池,所以它是否使用多核cpu的所有内核?

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?

推荐答案

它确实通过线程池利用多核。例如,在Linux上,底层的pthread将为多个线程使用多个内核。

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

如果运行以下代码,您会注意到4个(默认线程池大小)内核将挂起当文件系统IO与线程池一起运行时为100%。

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);
}

这篇关于nodejs中的libuv线程如何利用多核cpu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆