简体   繁体   English

NodeJS eventloop执行顺序

[英]NodeJS eventloop execution order

When sending several requests serially, it seems like 当连续发送几个请求时,似乎

  • callbacks are executed when all requests are sent. 发送所有请求时执行回调。
  • requests seems to get added into a queue but not actually getting executed before the loop is done. 请求似乎已添加到队列中,但在循环完成之前并未真正执行。

Example

var http = require('http');

var i=0;
var postData = [];

while (i++ < 12) {
    var largeObject = [
        'this' + i,
        'that' + i,
        'then' + i
    ];

    postData.push(largeObject);

    if (postData.length >= 2) {
        console.log('request');
        var options = {
            hostname: 'httpbin.org',
            port: 80,
            path: '/post',
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            }
        };
        var req = http.request(options, function(res) {
            console.log(res.statusCode);
            res.on('data', function(chunk) {
                console.log(chunk.toString());
            });

            res.on('end', function() {
                console.log('finished reading response');
            });
        });
        req.end(JSON.stringify(postData), function() {
            console.log('request stream finished');
        });

        postData = [];
    }
}

The output here would look like 这里的输出看起来像

request
request
request
request
request
request
request stream finished
request stream finished
request stream finished
request stream finished
request stream finished
request stream finished
200
{
  "args": {}, 
  "data": "[[\"this7\",\"that7\",\"then7\"],[\"this8\",\"that8\",\"then8\"]]", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Content-Length": "53", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org"
  }, 
  "json": [
    [
      "this7", 
      "that7", 
      "then7"
    ], 
    [
      "this8", 
      "that8", 
      "then8"
    ]
  ], 
  "origin": "xxxxx", 
  "url": "http://httpbin.org/post"
}

finished reading response
200
{
  "args": {}, 
  "data": "[[\"this1\",\"that1\",\"then1\"],[\"this2\",\"that2\",\"then2\"]]", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Content-Length": "53", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org"
  }, 
  "json": [
    [
      "this1", 
      "that1", 
      "then1"
    ], 
    [
      "this2", 
      "that2", 
      "then2"
    ]
  ], 
  "origin": "xxxx", 
  "url": "http://httpbin.org/post"
}
...

Is there any way to finish one request before the next one is getting executed? 有什么方法可以在执行下一个请求之前完成一个请求?

I don't really care about the order, it's more about memory - I'd like to get rid of large objects I am posting 我不太在意订单,而是更多地关注内存-我想摆脱发布的大对象

eg 例如

request1
response1
request2
response2
request3
response3
request4
request5
response5
response4

would be absolutely fine. 绝对没问题。 Any suggestion? 有什么建议吗?

Of course, just use some control-flow modules like async or bluebird. 当然,只需使用一些控制流模块,例如async或bluebird。

As you don't care about order, I'd advise you to use either async#parallel or bluebird#map . 由于您不在乎顺序,因此建议您使用async#parallelbluebird#map The bluebird#map has a concurrency argument, which can be nice when you need some more control over loops and their amount (so does async#parallelLimit ). bluebird#map有一个并发参数,当您需要对循环及其数量进行更多控制时,它会很不错( async#parallelLimit )。

If this doesn't seem straightforward, please comment and I'll add an example. 如果这看起来不简单,请发表评论,我将添加一个示例。

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

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