简体   繁体   English

Node.js如何在阻塞过程中处理多个并行请求

[英]Nodejs how to handle multiple parallel requests incase of blocking process

I am doing a small project in which a user makes a http request to my server after which I get and process some data from three different servers and then return the summed up result to the client. 我正在做一个小项目,其中用户向我的服务器发出http请求,然后我从三个不同的服务器获取并处理一些数据,然后将汇总结果返回给客户端。

Everything works fine accept when more than two clients make requests at same time, all clients are not able to get response and faces timeout error. 当两个以上的客户端同时发出请求时,所有客户端都无法正常工作,所有客户端均无法获得响应并面临超时错误。

How to handle this thing . 这个东西怎么处理。

Thanks in advance 提前致谢

Ok, i get it. 好的我明白了。 I would recommend using es6 promises for that kind of stuff. 我建议对此类内容使用es6 promises。 You can use request-promise and bluebird to make http request to other servers. 您可以使用request-promisebluebird向其他服务器发出http请求。

And then just: 然后:

var rp = require('request-promise');
var bluebird = require('bluebird');

bluebird
.all(rp('http://www.google.com'), rp('http://example.com'))
.then(function (googleResponse, exampleResponse) {
    // Some computation
    return someResult;
})
.then(function (previousResult) {
    // Some more computation
    return someResult;
});

Basically what you want is making part of computation, returning with then and allowing node to takeover, then in next then doing other part of computation and so on. 基本上,您想要的是进行计算的一部分,然后返回并允许节点接管,然后在下一步中进行计算的其他部分,依此类推。 If you split your process into few small blocks you should be able to have less blocking. 如果将流程分成几个小块,则应该可以减少阻塞。

Other than that you can checkout node clustering, it is quite simple process that allows you to handle server load between few workers: https://www.sitepoint.com/how-to-create-a-node-js-cluster-for-speeding-up-your-apps/ 除此之外,您还可以签出节点群集,这是一个非常简单的过程,可让您处理少数工作人员之间的服务器负载: https : //www.sitepoint.com/how-to-create-a-node-js-cluster-for -speeding -上-你-应用程序/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM