azro
azro

Reputation: 54148

Why async.each does not run the iterations in parallel?

To understand and look the behaviour of async.each I wrote some lines, but in my case it seems that the iterations are not run in parallel, is it because I'm using the console.log to check where the code is ?

JS Server

let server = http.createServer(function (req, res) {
    res.writeHead(200);
    let datas = [1, 2, 3, 4];
    async.each(datas, function (data, callback) {
        console.log(data);
        for (let i = 0; i < 1000 * 1000 * 1000 * 2; i++) { // the loop takes about 1-2sec
            let b = i * i;
            b += 2
        }
        console.log(data);
        callback()
    }, function (err) {
        console.log("callback ", err ? err : "");
        res.end('Hey');
    })
});
server.listen(8080);

it prints :

1
1
2
2
3
3
4
4
callback

The sources that explain why I expect the parallelism


WORKING EXAMPLE

A web request is non-blocking and does not take all the cpu so it works

const async = require('async');
const http = require('http');
const request = require('request');
const url = 'https://caolan.github.io/async/v3/docs.html'; // input your url here

let server = http.createServer(function (req, res) {
    res.writeHead(200);
    let datas = [1, 2, 3, 4];
    async.each(datas, function (data, callback) {
        console.log(data);
        request({url: url}, function (err, res, body) {
            const rr = body.indexOf("Processing") + body.indexOf("until");
            console.log(data, " ", new Date().getTime());
        });
        callback()
    }, function (err) {
        console.log("callback ", err ? err : "");
        res.end('Salut tout le monde !');
    })
});
server.listen(8080);

Gives

1
2
3
4
callback
1 ' ' 1565298694448
3 ' ' 1565298694555
4 ' ' 1565298694569
2 ' ' 1565298694576

Upvotes: 0

Views: 130

Answers (1)

tadman
tadman

Reputation: 211590

If your loops are CPU bound, you're going to pin the process and there's no chance to yield for the next task. This isn't non-blocking code, this blocks, so it'll be sequential.

Remember, JavaScript code never really runs in parallel, it's locked to one thread under normal conditions, it just runs concurrently. To get that to happen your code must yield, it can't monopolize the thread.

Upvotes: 1

Related Questions