瀑布为什么这么慢?

发布于 2024-10-07 20:45:41 字数 831 浏览 5 评论 0原文

我正在为 Node 使用异步模块(请参阅 https://github.com/caolan/async)。我的问题是...为什么瀑布这么慢?

执行这段代码大约需要4秒...

App.post("/form", function(request, response) {

Async.waterfall([

  function(callback) {

    console.log("1.");
    callback(null, "some data");

  },

  function(data, callback) {

    console.log("2.");            
    callback(null, "some data");

  },

  function(data, callback) {

    console.log("3.");
    callback(null, "some data");              

  }

], function(error, document) {

  console.log("4.");            
  console.log("Done.");

  response.send(); // Takes 4 seconds

});

}

输出

1.
2.
// After 4 seconds
3.
4.
Done.

谢谢回复!

I'm using async module (see https://github.com/caolan/async) for Node.js and my question is... Why is waterfall so slow?

It takes about 4 seconds to execute this piece of code...

App.post("/form", function(request, response) {

Async.waterfall([

  function(callback) {

    console.log("1.");
    callback(null, "some data");

  },

  function(data, callback) {

    console.log("2.");            
    callback(null, "some data");

  },

  function(data, callback) {

    console.log("3.");
    callback(null, "some data");              

  }

], function(error, document) {

  console.log("4.");            
  console.log("Done.");

  response.send(); // Takes 4 seconds

});

}

Output

1.
2.
// After 4 seconds
3.
4.
Done.

Thanks for reply!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这只是另一个 Node.js 错误。

在待处理的 http.ServerResponse 期间,在另一个 process.nextTick 中使用 process.nextTick 会被破坏。

var http = require('http');
http.createServer(function(req, res) {
    var now = new Date();
    process.nextTick(function() {
        process.nextTick(function() {
            console.log(new Date() - now);
            res.writeHead({});
            res.end('foooooo');
        });
    });
}).listen(3000);

这需要很长时间,async.js 从其他回调内部调用回调,这些回调是通过 process.nextTick 调用的,然后导致触发上述错误。

快速修复:async.js63中修改async.nextTick以仅使用setTimeout.

错误:我已就此提交了问题

It's just another Node.js Bug.

Using process.nextTick inside another process.nextTick during a pending http.ServerResponse is broken.

var http = require('http');
http.createServer(function(req, res) {
    var now = new Date();
    process.nextTick(function() {
        process.nextTick(function() {
            console.log(new Date() - now);
            res.writeHead({});
            res.end('foooooo');
        });
    });
}).listen(3000);

This takes an eternity, async.js calls the callbacks from inside the other callbacks which were called via process.nextTick which then results in the above bug being triggered.

Quick fix: In async.js line 63 modifiy async.nextTick to only use setTimeout.

Bug: I've filed an issue on this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文