kietheros
kietheros

Reputation: 463

Nodejs redis method BLPOP blocks the event-loop

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.

Upvotes: 3

Views: 1292

Answers (2)

user5365075
user5365075

Reputation: 2279

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

Upvotes: 1

AnonyMouze
AnonyMouze

Reputation: 416

i currently cannot comment so i will have to post it here. blpop will block when there is no data. Another thing to consider is the Redis's version . blpop behave differently between versions. You can refer to redis documentation .

Upvotes: 0

Related Questions