Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs redis method BLPOP blocks the event-loop

Tags:

node.js

redis

I use the library node-redis: https://github.com/NodeRedis/node-redis

let client = redis.createClient();
let blpopAsync = util.promisify(client.blpop).bind(client);
let rpushAsync = util.promisify(client.rpush).bind(client);

async function consume() {
    let data = await blpopAsync('queue', 0); // the program is blocked at here
}

async function otherLongRunningTasks() {
    // call some redis method
    await rpushAsync('queue', data);
}

I expect that method blpopAsync will be blocked until a element is popped. And in this time, event loop will run other async functions. However, my program is blocked at await blpopAsync forever.

Could you tell me how to use this function correctly to not block event-loop?


I find that blpop blocks other non-blocking methods of redis client, so other functions which use redis will wait forever. The event loop is still running.

like image 217
shang12 Avatar asked Jul 31 '26 09:07

shang12


1 Answers

As @AnonyMouze mentioned, doing client.duplicate().blpop(...) is the trick to freeing the connection.

like image 178
user5365075 Avatar answered Aug 02 '26 14:08

user5365075



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!