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.queue max length? #1022
Comments
There are no limits on the queue length -- only the practical limits of a JS array. I think you've uncovered a inconsistency in how queue.length() is reported though. 🐛 |
Is there any way to "lock" a separate variable in NodeJS to prevent this situation? |
You shouldn't need to "lock" a variable in JS since it is single-threaded -- there's only a single thread writing and accessing variables. You might have a tricky order-of-execution problem. Try placing a breakpoint on that line. |
True, but if I add 5 tasks to async.queue and they all finish at the same time and try to increment/decrement a var on callback, isn't that a race condition? |
It depends. There is nothing that should cause a race condition here -- execution can't be preempted: // queue is length 9, max is 10
if(queue.length() < maxLength){
// length is still 9
queue.push(item) // add an item
// length is now 10
} Out of curiosity, what version of async are you using? |
A more concrete test case would be useful here. |
Code : /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var Q = async.queue(function(line, callback) {
// I have an async call here. Eg- CallDbAndGetData(line);
callback();
}, 10);
var s = fs.createReadStream('C:/idcards/dev/scripts/XXXXXXXXXX.txt').pipe(es.split()).pipe(es.mapSync(function(line) {
Q.push(line, function(err) {
// console.log('finished pushing '+ line);
});
}));
Q.drain = function() {
console.log("All CallDbAndGetData calls were done");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// Problem statement: |
A |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var Q = async.queue(function(line, callback) {
// I have an async call here. Eg- CallDbAndGetData(line);
if(q._tasks.length < 100)
s.resume();
callback();
}, 10);
var s = fs.createReadStream('C:/idcards/dev/scripts/XXXXXXXXXX.txt').pipe(es.split()).pipe(es.mapSync(function(line) {
Q.push(line, function(err) {
// console.log('finished pushing '+ line);
if(q._tasks.length >= 100) // restricting q length
s.pause();
});
}));
Q.drain = function() {
console.log("All CallDbAndGetData calls were done");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// This is working fine for my case. Where q's in-memory size will not be huge and make the system slow. |
Is there any way to prevent/enforce a max async.queue length? I realize it's possible to set the concurrency level, but want to prevent more than a certain number of elements from being added to the queue.
Tried using an if statement with queue.length(), but more elements are added than wanted, due to race conditions.
More info: queue.length() is checked after every task is finished, in order to see if more work can be added. If two or more tasks finish at the same time check queue.length() and add work, queue.length() can go beyond maxLength.
The text was updated successfully, but these errors were encountered: