Javascript Node.js是否创建模块以收集内存(ram)信息?
我要做的是在Windows机器上,在Node.js中每隔X秒(在本例中仅为1秒)打印一次本地内存使用情况。具有实际收集该数据功能的代码需要位于单独的模块中。这是我当前的代码: 在Javascript Node.js是否创建模块以收集内存(ram)信息?,javascript,node.js,Javascript,Node.js,我要做的是在Windows机器上,在Node.js中每隔X秒(在本例中仅为1秒)打印一次本地内存使用情况。具有实际收集该数据功能的代码需要位于单独的模块中。这是我当前的代码: 在server.js中: mem_stats = require("./mem_stats.js"); setInterval(function () { mem_stats.update(); console.log(mem_stats.mem_total); }, 1000); var exec = req
server.js
中:
mem_stats = require("./mem_stats.js");
setInterval(function () {
mem_stats.update();
console.log(mem_stats.mem_total);
}, 1000);
var exec = require("child_process").exec,
mem_total,
mem_avail,
mem_used;
exports.update = function () {
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
mem_total = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
exec("wmic OS get FreePhysicalMemory", function (error, stdout, stderr) {
mem_avail = parseInt(stdout.split("\r\n")[1]) / 1048576; // 1024^2
});
}
exports.mem_total = mem_total;
exports.mem_avail = mem_avail;
exports.mem_used = mem_total - mem_avail;
在mem_stats.js
中:
mem_stats = require("./mem_stats.js");
setInterval(function () {
mem_stats.update();
console.log(mem_stats.mem_total);
}, 1000);
var exec = require("child_process").exec,
mem_total,
mem_avail,
mem_used;
exports.update = function () {
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
mem_total = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
exec("wmic OS get FreePhysicalMemory", function (error, stdout, stderr) {
mem_avail = parseInt(stdout.split("\r\n")[1]) / 1048576; // 1024^2
});
}
exports.mem_total = mem_total;
exports.mem_avail = mem_avail;
exports.mem_used = mem_total - mem_avail;
我怀疑(/相当肯定)这与JS的异步方式有关,但我似乎找不到一种解决方法(回调等)。到现在为止,我已经尝试了很多事情,但是无论我做什么,我总是以一个未定义的被打印出来
将我的代码更改为类似的内容也无法解决任何问题:
function mem_total () {
var temp;
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
return temp;
};
function mem_avail () {
var temp;
exec("wmic OS get FreePhysicalMemory", function (error, stdout, stderr) {
temp = parseInt(stdout.split("\r\n")[1]) / 1048576; // 1024^2
});
return temp;
};
exports.mem_total = mem_total();
exports.mem_avail = mem_avail();
我就是不明白
我知道这个问题看起来(相当)有点愚蠢,但我对编写JS没有太多经验,我非常习惯于使用更多面向C++的语言。不过还是要感谢您。对于第二个示例,该命令将按以下方式执行
function mem_total () {
var temp;
// call exec now. Since it is async, When the function finishes,
// call the callback provided
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER mem_total has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
// return temp before exec finishes.
return temp;
};
mem_total(function(err, mem) {
if (err) {
console.log(err);
return;
}
console.log('total memory is ', mem);
});
也许你想要以下的东西:
function mem_total (callback) {
var temp;
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER the function has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
callback(error, temp);
});
};
并按以下方式调用该函数
function mem_total () {
var temp;
// call exec now. Since it is async, When the function finishes,
// call the callback provided
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER mem_total has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
// return temp before exec finishes.
return temp;
};
mem_total(function(err, mem) {
if (err) {
console.log(err);
return;
}
console.log('total memory is ', mem);
});
对于第二个示例,该命令将按以下方式执行
function mem_total () {
var temp;
// call exec now. Since it is async, When the function finishes,
// call the callback provided
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER mem_total has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
// return temp before exec finishes.
return temp;
};
mem_total(function(err, mem) {
if (err) {
console.log(err);
return;
}
console.log('total memory is ', mem);
});
也许你想要以下的东西:
function mem_total (callback) {
var temp;
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER the function has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
callback(error, temp);
});
};
并按以下方式调用该函数
function mem_total () {
var temp;
// call exec now. Since it is async, When the function finishes,
// call the callback provided
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER mem_total has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
// return temp before exec finishes.
return temp;
};
mem_total(function(err, mem) {
if (err) {
console.log(err);
return;
}
console.log('total memory is ', mem);
});
对于第二个示例,该命令将按以下方式执行
function mem_total () {
var temp;
// call exec now. Since it is async, When the function finishes,
// call the callback provided
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER mem_total has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
// return temp before exec finishes.
return temp;
};
mem_total(function(err, mem) {
if (err) {
console.log(err);
return;
}
console.log('total memory is ', mem);
});
也许你想要以下的东西:
function mem_total (callback) {
var temp;
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER the function has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
callback(error, temp);
});
};
并按以下方式调用该函数
function mem_total () {
var temp;
// call exec now. Since it is async, When the function finishes,
// call the callback provided
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER mem_total has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
// return temp before exec finishes.
return temp;
};
mem_total(function(err, mem) {
if (err) {
console.log(err);
return;
}
console.log('total memory is ', mem);
});
对于第二个示例,该命令将按以下方式执行
function mem_total () {
var temp;
// call exec now. Since it is async, When the function finishes,
// call the callback provided
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER mem_total has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
// return temp before exec finishes.
return temp;
};
mem_total(function(err, mem) {
if (err) {
console.log(err);
return;
}
console.log('total memory is ', mem);
});
也许你想要以下的东西:
function mem_total (callback) {
var temp;
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER the function has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
callback(error, temp);
});
};
并按以下方式调用该函数
function mem_total () {
var temp;
// call exec now. Since it is async, When the function finishes,
// call the callback provided
exec("wmic ComputerSystem get TotalPhysicalMemory", function (error, stdout, stderr) {
// temp is modified AFTER mem_total has returned
temp = parseInt(stdout.split("\r\n")[1].toString()) / 1073741824; // 1024^3
});
// return temp before exec finishes.
return temp;
};
mem_total(function(err, mem) {
if (err) {
console.log(err);
return;
}
console.log('total memory is ', mem);
});
虽然这与您的问题无关,但是os
模块包含了更简单的方法,可用于您在这里执行的操作os.totalmem()
返回系统内存的字节数,os.freemem()
返回可用内存量。我知道这一点,但问题是,我也在尝试为CPU使用率/温度和GPU使用率/温度这样做。然而,这些模块有点复杂,因为里面有更多的信息,所以用RAM模块显示问题是最简单/最直接/最干净的。尽管这与您的问题无关,os
模块包含了更简单的方法来说明您在这里所做的事情os.totalmem()
返回系统内存的字节数,os.freemem()
返回可用内存量。我知道这一点,但问题是,我也在尝试为CPU使用率/温度和GPU使用率/温度这样做。然而,这些模块有点复杂,因为里面有更多的信息,所以用RAM模块显示问题是最简单/最直接/最干净的。尽管这与您的问题无关,os
模块包含了更简单的方法来说明您在这里所做的事情os.totalmem()
返回系统内存的字节数,os.freemem()
返回可用内存量。我知道这一点,但问题是,我也在尝试为CPU使用率/温度和GPU使用率/温度这样做。然而,这些模块有点复杂,因为里面有更多的信息,所以用RAM模块显示问题是最简单/最直接/最干净的。尽管这与您的问题无关,os
模块包含了更简单的方法来说明您在这里所做的事情os.totalmem()
返回系统内存的字节数,os.freemem()
返回可用内存量。我知道这一点,但问题是,我也在尝试为CPU使用率/温度和GPU使用率/温度这样做。然而,这些模块有点复杂,因为里面有更多的信息,所以用RAM模块解决问题是最简单/最直接/最干净的。这是一个很好的答案,但是在创建节点回调时,应该始终使用标准签名,回调(错误,结果)
。这是一个很好的答案,但在创建节点回调时,应始终使用标准签名,回调(错误,结果)
。这是一个很好的答案,但在创建节点回调时,应始终使用标准签名,回调(错误,结果)
。这是一个很好的答案,但是,创建节点回调时,应始终使用标准签名,callback(error,results)
。