Node.js的,异步模块,并发 [英] Node.js, async module, concurrency

查看:179
本文介绍了Node.js的,异步模块,并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是最好避免使用异步模块功能的多个实例在同一时间?

Am I best to avoid using more than one instance of an async module feature at a time?

我code的在顺序进行三个部分。我使用code像

My code has three parts to be performed in sequence. I'm using code like

var async = require('async');
async.waterfall(
  [ function(cb) { part1(); },
    function(cb) { part2(); },
    function(cb) { part3(); }
  ],
  function(err, res) { console.log('Done'); }
);

第一部分()有三个部分,太。这些运行的前两个之后的第三。我的想法是使用

part1() has three parts, too. The third of those runs after the first two. My thought was to use

function part1(err, res) {
  async.waterfall(
    [ function(cb) {
        async.parallel(
          [ function(cb) { part1a(); },
            function(cb) { part1b(); }
          ]
        );
      },
      function(cb) { part1c(); },
      function(cb) {
        if (err) return err('part 1 error')
        return res()
      }
    ]
  );
}

在哪里part1a和part1b正在运行,我使用的是异步的三个特点:外瀑布,并在第一部分瀑布和并行。我第一部分重组只使用一个特征:瀑布,然后part1a part1b然后part1c只是为了得到它的工作。

Where part1a and part1b are running, I'm using three features of async: the outer waterfall, and the waterfall and parallel in part1. I restructured part1 to use only one feature: a waterfall, part1a then part1b then part1c just to get it working.

我没有取得成功。举例来说,我还没有看到完成的消息。当我第一部分改制,我得到part1a一个电话,但不是part1b或任何其他。

I am not achieving success. For example, I haven't seen a message 'Done'. And when I restructured part1, I get a call on part1a, but not on part1b or any other.

也许我并不孤独。在<一个href=\"http://stackoverflow.com/questions/31097142/optional-callback-not-being-called-in-the-node-js-async-modules-foreachof-metho\">Optional回调不被称为Node.js的异步模块的forEachOf方法,@LuisDelgado表示,作为我个人而言,我还没有在其与具有内部回调异步函数异步很好的工作必须到目前为止成功函数本身。

Maybe I'm not alone. In Optional callback not being called in the node.js async module's forEachOf method, @LuisDelgado indicated, "As a personal note, I have not had so far success in having async work nicely with a function that has an internal callback-async function itself."

在<一个href=\"http://stackoverflow.com/questions/32029038/complicated-use-case-for-node-js-async-module\">Complicated用例Node.js的异步模块,@Robbie和@dark_shadow发现一些成功的async.auto。我想这将涉及到打破我的三个外层功能集成到它们的组件,并呼吁从外部层面的每个组件拼合我的code。也许这就是JavaScript的方式!

In Complicated use case for Node.js Async Module, @Robbie and @dark_shadow found some success with async.auto. I guess that would involve flattening my code by breaking my three outer level functions into their components and calling each component from the outer level. Maybe that's "the JavaScript way"!

什么是正确的?

在此先感谢...

编辑:干得好,亚当·萨克斯。感谢很多!一个后续的...

Nice job, Adam Sax. Thanks lots! A follow-on ...

编辑:亚当和其他人提出的承诺。亚当喜欢蓝鸟,显示一些有用的语法。我有麻烦与蓝鸟,呼吁虽然它是。我在这里发表后续蓝鸟问题。有了这个编辑,我去除后续,留下这个问题(如标题所暗示的)作为一个异步的问题。

Adam and others suggested promises. Adam likes bluebird and showed some useful syntax. I'm having trouble with bluebird, appealing though it is. I posted a follow-on bluebird question here. With this edit, I'm removing that follow-on, leaving this question (as the title suggests) as an async question.

对于那些鼓励我用用,谢谢!请参阅<一个href=\"http://stackoverflow.com/questions/33858732/node-js-bluebird-poor-control-of-execution-path\">node.js,蓝鸟,不好控制执行路径的。

For those encouraging me to use promises, thanks! Please see node.js, bluebird, poor control of execution path.

异步的问题是:我是最好避免使用异步模块功能的多个实例在同一时间

The async question remains: Am I best to avoid using more than one instance of an async module feature at a time?

推荐答案

我也建议在这种情况下使用的承诺,因为他们好得多 - 少code和执行和错误处理的单链。如果你想坚持使用异步,你需要调用传递给异步方法回调时,你的异步操作完成。

I would also recommend using promises in this case because they are much nicer -- less code and a single chain of execution and error handling. If you want to stick with async, you need to call the callbacks passed to the async methods when your asynchronous operations are done.

var async = require('async');
async.waterfall(
  [ function(cb) { part1(cb); },
    function(cb) { part2(cb); },
    function(cb) { part3(cb); }
  ],
  function(err, res) { console.log('Done'); }
);

function part1(callbackFromAsync) {
  async.waterfall(
    [ function(waterfallCallback) {
        async.parallel(
          [ function(cb) { part1a(cb); },
            function(cb) { part1b(cb); }
          ], waterfallCallback // <- this needs to be called
        );
      },
      function(cb) { part1c(cb); },
      function(cb) {
          callbackFromAsync(); // <- this needs to be called!
      }
    ]
  );
}

实际上,每一次的异步操作完成一个 CB 函数需要调用以指示其完成。试想一下,第2部分第3部分 part1a 等等得多看起来像这样简单的功能:

Practically, each time an asynchronous operation is complete a cb function needs to be called to signal its completion. Imagine that part2, part3, part1a etc. are much simpler functions that look like this:

function part(callback) {
  someAsyncOperation(function (err, result) {
    callback(err, result);
  });
}

您必须调用传递到了函数信号异步操作完成回调。您可以使用回调来传递错误(如果有的话),结果(如果需要)备份功能堆栈。 回调的呼召是如何异步了解您的异步函数完成。

You have to call the callback passed into that function to signal the async operation is complete. You use the callback to pass the error (if any) and the result (if needed) back up the function stack. The calling of callback is how async knows your asynchronous function is done.

这篇关于Node.js的,异步模块,并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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