提问者:小点点

异步生成器中由于非并行等待许诺而引起的减速


我正在使用生成器和蓝鸟编写代码,我有以下内容:

var async = Promise.coroutine;
function Client(request){
    this.request = request;
}


Client.prototype.fetchCommentData = async(function* (user){
    var country = yield countryService.countryFor(user.ip);
    var data = yield api.getCommentDataFor(user.id);
    var notBanned = yield authServer.authenticate(user.id);
    if (!notBanned) throw new AuthenticationError(user.id);
    return {
        country: country,
        comments: data,
        notBanned: true
    };
});

但是,这有点慢,我觉得我的应用程序等待I/O太多了,而且它不是并行的。如何提高应用程序的性能?

对于,总响应时间为800;对于,总响应时间为400;对于,总响应时间为600。


共3个答案

匿名用户

您正在花费太多时间等待来自不同来源的I/O。

在普通的promise代码中,您会为此使用,但是,人们倾向于编写带有生成器的等待请求的代码。您的代码执行以下操作:

<-client     service->
countryFor..
           ''--..
              ''--..
                 ''--.. country server sends response
               ..--''
          ..--''
     ..--''
getCommentDataFor
     ''--..
           ''--..
               ''--..
                     ''--.. comment service returns response
                ..--''
          ..--''
      ..--''
authenticate
       ''--..
            ''--..
                  ''--.. authentication service returns
             ..--''
       ..--''
 ..--''
 Generator done.

相反,它应该做的是:

<-client     service->
countryFor..
commentsFor..''--..
authenticate..''--..''--..
                 ''--..''--..''--.. country server sends response
                        ''--..--''..  comment service returns response
                   ..--''..--''..     authentication service returns response
          ..--''..--''..
 ..--''..--''..--''
 ..--''..--''
 ..--''
 Generator done

简单地说,您所有的I/O都应该在这里并行完成。

为了解决这个问题,我使用获取一个对象并等待解析其所有属性(如果它们是promises)。

记住--生成器和承诺很好地结合在一起,你只需做出承诺:

Client.prototype.fetchCommentData = async(function* (user){
    var country = countryService.countryFor(user.ip);
    var data = api.getCommentDataFor(user.id);
    var notBanned = authServer.authenticate(user.id).then(function(val){
          if(!val) throw new AuthenticationError(user.id);
    });
    return Promise.props({ // wait for all promises to resolve
        country : country,
        comments : data,
        notBanned: notBanned
    });
});

这是人们第一次使用发电机时会犯的一个非常常见的错误。

ascii艺术无耻地取自Q-Connection(Kris Kowal)

匿名用户

正如的Bluebird文档中提到的,您需要注意不要将放在一个系列中。

var county = yield countryService.countryFor(user.ip);
var data = yield api.getCommentDataFor(user.id);
var notBanned = yield authServer.authenticate(user.id);

这段代码有3个表达式,每个表达式都停止执行,直到特定的承诺得到解决。代码将连续创建和执行每个异步任务。

要并行等待多个任务,您应该一个承诺数组。这将一直等到它们全部结算,然后返回一个结果值数组。使用ES6析构赋值可以得到简洁的代码:

Client.prototype.fetchCommentData = async(function* (user){
    var [county, data, notBanned] = yield [
//             a single yield only: ^^^^^
        countryService.countryFor(user.ip),
        api.getCommentDataFor(user.id),
        authServer.authenticate(user.id)
    ];
    if (!notBanned)
        throw new AuthenticationError(user.id);
    return {
        country: country,
        comments: data,
        notBanned: true
    };
});

匿名用户

Benjamin Gruenbaum的答案是正确的,但它完全失去了生成器方面,当您试图并行运行多个事物时,这往往会发生一点。但是,使用关键字可以很好地完成这项工作。我还使用了一些额外的ES6特性,比如销毁赋值和对象初始值速记:

Client.prototype.fetchCommentData = async(function* (user){
    var country = countryService.countryFor(user.ip);
    var data = api.getCommentDataFor(user.id);
    var notBanned = authServer.authenticate(user.id).then(function(val){
        if(!val) throw new AuthenticationError(user.id);
    });

    // after each async operation finishes, reassign the actual values to the variables
    [country, data, notBanned] = yield Promise.all([country, data, notBanned]);

    return { country, data, notBanned };
});

如果您不想使用这些额外的ES6特性:

Client.prototype.fetchCommentData = async(function* (user){
    var country = countryService.countryFor(user.ip);
    var data = api.getCommentDataFor(user.id);
    var notBanned = authServer.authenticate(user.id).then(function(val){
        if(!val) throw new AuthenticationError(user.id);
    });

    var values = yield Promise.all([country, data, notBanned]);

    return { 
        country: values[0], 
        data: values[1], 
        notBanned: values[2]
    };
});