node.time中的console.time()安全吗? [英] Is console.time() safe in node.js?

查看:87
本文介绍了node.time中的console.time()安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面前有一小段node.js代码,如下所示:

I have a little snippet of node.js code in front of me that looks like this:

console.time("queryTime");
doAsyncIOBoundThing(function(err, results) {
    console.timeEnd("queryTime");
    // Process the results...
});

当然,当我在我的(否则是空闲的)开发系统上运行它时,我得到了一个不错的控制台消息如下:

And of course when I run this on my (otherwise idle) development system, I get a nice console message like this:

queryTime: 564ms

但是,如果我把它投入生产,那么同时进行的异步调用是否会有多次,并且每个异步调用都会覆盖以前的计时器?或节点是否有某种神奇的执行上下文,为每个执行线程提供一个单独的控制台计时器名称空间?

However, if I put this into production, won't there likely be several async calls in progress simultaneously, and each of them will overwrite the previous timer? Or does node have some sort of magical execution context that gives each "thread of execution" a separate console timer namespace?

推荐答案

使用独特的标签,这将是安全的。这就是你使用标签来唯一识别开始时间的原因。

Just use unique labels and it will be safe. That's why you use a label, to uniquely identify the start time.

只要你不小心使用标签两次,一切都会按照预期完成。另请注意,节点通常只有一个执行线程。

As long as you don't accidentally use a label twice everything will work exactly as intended. Also note that node has usually only one thread of execution.

这篇关于node.time中的console.time()安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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