Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async.parallel is not working if I don't use setTimeout #4

Closed
rbhogi opened this issue Nov 11, 2010 · 4 comments
Closed

async.parallel is not working if I don't use setTimeout #4

rbhogi opened this issue Nov 11, 2010 · 4 comments

Comments

@rbhogi
Copy link

rbhogi commented Nov 11, 2010

Hi,

I have following code. This is working as expected if I use setTimeOout. If I simulate delay instead of using setTimeout, parallization is not working.

var async = require('async');

function getTasks(useTimeout){
return [
function(callback){
task("task 1", 500, callback, useTimeout)
}
,
function(callback){
task("task 2", 400, callback, useTimeout)
}
];
}

function task(name, delay, callback, useTimeout){
if(useTimeout){
console.log(name + "(" + delay +")");
setTimeout(function(){
callback(null, name);
},delay);
}else{
console.log(name + "(" + pause(delay) +")");
callback(null, name);
}
}

function callback(err, results){
console.log("async.parallel : "+ (new Date()-start) + "\n");
}

function pause(delay){
var start = new Date();
var len = 300000*delay;
for(var i=0;i<len;i++){};
return (new Date()-start);
}

var start = new Date();
//async.parallel(getTasks(false),callback);
async.parallel(getTasks(true),callback);

@caolan
Copy link
Owner

caolan commented Nov 11, 2010

remember that you only have a single thread of execution, so without using setTimeout the pause() function will block. Using async.parallel will not make your javascript asynchronous, rather it will help you properly manage asynchronous operations.

If you need to run javascript like this in parallel you'll need to use multiple processes or look into web workers.

@rbhogi
Copy link
Author

rbhogi commented Nov 12, 2010

I tried setTimeout for pause case also. But it didn't work.
Here is the code for your reference

function task(name, delay, callback, useTimeout){
if(useTimeout){
console.log(name + "(" + delay +")");
setTimeout(function(){
callback(null, name);
},delay);
}else{
setTimeout(function(){
console.log(name + "(" + pause(delay) +")");
callback(null, name);
},1);
}
}

@rbhogi
Copy link
Author

rbhogi commented Nov 12, 2010

I'll look into web workers. Thanks for quick response.

@rbhogi
Copy link
Author

rbhogi commented Nov 12, 2010

I have following simple program. I am looking to parallelize this program using web-worker.

function process(delay){

var start = new Date();
var len = 300000*delay;
for(var i=0;i<len;i++){};
return (new Date()-start);
}

var start = new Date();
process(500);
process(600);
console.log("Time taken :"+(new Date()-start));

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants