gpt4 book ai didi

javascript - 如何监视nodejs进程

转载 作者:太空宇宙 更新时间:2023-11-04 01:15:35 25 4
gpt4 key购买 nike

我正在运行一个需要很长时间的nodejs脚本。它运行一个很长的 for 循环并进行一些计算。最后它会显示答案。答案可能不是 42,而是一个 5 位数字。我删除了显示正在执行的操作的 console.log 消息,以使其运行得更快。

我想知道它已经发展到什么程度了。有什么方法可以查看流程并查看循环的进度,或者任何变量中包含哪些值?我不想打断它,因为我使用 time 命令运行它来计算获得答案所需的时间。

循环10亿次后,大约用了9分钟才结束。你认为循环1万亿次需要多长时间?

最佳答案

创建自定义repl并通过 Node 程序中的套接字公开它。然后,您可以远程登录到您的进程,并且您将可以访问任何全局变量以及暴露给 repl 上下文的任何变量。例如:

var net = require("net")
, repl = require("repl");

var counterOfInterest = 0;
setInterval(function() { counterOfInterest++; }, 1000);
function whatsTheStatus() {
return 'process has counted ' + counterOfInterest + ' times'
};

net.createServer(function (socket) {
var remote = repl.start("remote ➜ ", socket);
remote.context.whatsTheStatus = whatsTheStatus;
}).listen(8888);

然后,您可以远程登录到您的 repl 并与您的 Node 程序交互!

$ telnet 127.0.01  8888
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
remote ➜ whatsTheStatus()
'process has counted 9 times'
remote ➜ whatsTheStatus()
'process hascounted 12 times'

关于javascript - 如何监视nodejs进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com