node.js 的 console.log 是异步的吗? [英] is node.js' console.log asynchronous?

查看:34
本文介绍了node.js 的 console.log 是异步的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

node.js 中的 console.log/debug/warn/error 是否异步?我的意思是 javascript 代码执行会停止直到这些东西打印在屏幕上还是会在稍后阶段打印?

此外,我有兴趣知道如果在节点崩溃后立即执行该语句,console.log 是否可能不显示任何内容.

解决方案

更新:从 Node 0.6 开始,这篇文章已经过时,因为 stdout 现在是同步.

好吧,让我们看看 console.log 实际上做了什么.

首先它是控制台模块的一部分:

exports.log = function() {process.stdout.write(format.apply(this, arguments) + '
');};

所以它只是做了一些格式化并写入process.stdout,到目前为止没有异步.

process.stdout 是一个 getter 在启动时定义 这是延迟初始化的,我添加了一些注释来解释:

.... 这里的代码...process.__defineGetter__('stdout', function() {如果(标准输出)返回标准输出;//只初始化一次///这里需要很多...if (binding.isatty(fd)) {//一个终端?伟大的!标准输出 = 新 tty.WriteStream(fd);} else if (binding.isStdoutBlocking()) {//一个文件?stdout = new fs.WriteStream(null, {fd: fd});} 别的 {stdout = new net.Stream(fd);//一个流?//例如:节点 foo.js >输出.txt标准输出.可读=假;}返回标准输出;});

在 TTY 和 UNIX 的情况下,我们最终 这里,这个东西继承自socket.因此,该节点基本上所做的就是将数据推送到套接字,然后终端会负责其余的工作.

让我们测试一下!

var data = '1111111111111111111111111111111111111111111111111111';for(var i = 0, l = 12; i 

结果

....很多.....1111111111111111在 17ms 内写了 208896 个字节真实 0m0.969s用户 0m0.068s系统 0m0.012s

终端打印出sockets内容大约需要1秒,而node只需要17毫秒就可以将数据推送到终端.

流案例也是如此,文件案例也得到处理 异步.

所以Node.js 信守其非阻塞承诺.

Are console.log/debug/warn/error in node.js asynchrounous? I mean will javascript code execution halt till the stuff is printed on screen or will it print at a later stage?

Also, I am interested in knowing if it is possible for a console.log to NOT display anything if the statement immediately after it crashes node.

解决方案

Update: Starting with Node 0.6 this post is obsolete, since stdout is synchronous now.

Well let's see what console.log actually does.

First of all it's part of the console module:

exports.log = function() {
  process.stdout.write(format.apply(this, arguments) + '
');
};

So it simply does some formatting and writes to process.stdout, nothing asynchronous so far.

process.stdout is a getter defined on startup which is lazily initialized, I've added some comments to explain things:

.... code here...
process.__defineGetter__('stdout', function() {
  if (stdout) return stdout;                            // only initialize it once 

  /// many requires here ...

  if (binding.isatty(fd)) {                             // a terminal? great!
    stdout = new tty.WriteStream(fd);
  } else if (binding.isStdoutBlocking()) {              // a file?
    stdout = new fs.WriteStream(null, {fd: fd});
  } else {
    stdout = new net.Stream(fd);                        // a stream? 
                                                        // For example: node foo.js > out.txt
    stdout.readable = false;
  }

  return stdout;
});

In case of a TTY and UNIX we end up here, this thing inherits from socket. So all that node bascially does is to push the data on to the socket, then the terminal takes care of the rest.

Let's test it!

var data = '111111111111111111111111111111111111111111111111111';
for(var i = 0, l = 12; i < l; i++) {
    data += data; // warning! gets very large, very quick
}

var start = Date.now();
console.log(data);
console.log('wrote %d bytes in %dms', data.length, Date.now() - start);

Result

....a lot of ones....1111111111111111
wrote 208896 bytes in 17ms

real    0m0.969s
user    0m0.068s
sys  0m0.012s

The terminal needs around 1 seconds to print out the sockets content, but node only needs 17 milliseconds to push the data to the terminal.

The same goes for the stream case, and also the file case gets handle asynchronous.

So yes Node.js holds true to its non-blocking promises.

这篇关于node.js 的 console.log 是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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