Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

javascript - How to execute functions in parallel with async.js?

In the following code, I have Array.forEach, It executes the doSomething synchronous function in sequence:

items.forEach(function(item) {
doSomething(item);
});

I need to execute functions (doSomething) in parallel, use async.js and try the following:

async.each(items, function (item, doneCallback) {
                        var startDate = new Date();
                        console.log(item.name().toString() + ' startDate: ' + startDate.toString() + ' - ' + startDate.getMilliseconds().toString());
                        doSomething(item); //Lazy function for many operations.
                        var endDate = new Date();
                        console.log(item.name().toString() + ' endDate' + endDate.toString() + ' - ' + endDate.getMilliseconds().toString());

                        return doneCallback(null);

                    }, function (err) {
                        otherFunction();
                        console.log('Finished');
                    });

But function doSomething was executed in sequence.

I had tried with async.parallel, but function doSomething was executed in sequence again:

items.forEach(function (item) {
                        var func = function (doneCallback) {
                            var startDate = new Date();
                            console.log(item.name().toString() + ' startDate: ' + startDate.toString() + ' - ' + startDate.getMilliseconds().toString());

                            doSomething(item); //Lazy function for many operations.

                            var endDate = new Date();
                            console.log(item.name().toString() + ' endDate' + endDate.toString() + ' - ' + endDate.getMilliseconds().toString());

                            return doneCallback(null);
                        };
                        functions.push(func);
                    });

                    async.parallel(functions, function (err, results) {
                        otherFunction();
                        console.log('Finished');
                    });

How to execute doSomething synchronous function in parallel with async.js?

Please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately Javascript is a single threaded language. However, HTML5 aims to improve this by adding Web Workers, which allows real OS-level threads to be spawned. If you are able to leverage HTML5 then this may work for you. You would be able to spawn a Web Worker for each item then wait until all threads are complete (joined).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...