gpt4 book ai didi

node.js - Nodejs redis 方法 BLPOP 阻塞事件循环

转载 作者:行者123 更新时间:2023-12-03 06:44:42 26 4
gpt4 key购买 nike

我使用库 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);
}

我预计 blpopAsync 方法将被阻塞,直到元素被弹出。而此时,事件循环会运行其他异步函数。但是,我的程序永远被阻止在 await blpopAsync

你能告诉我如何正确使用这个函数而不阻塞事件循环吗?


我发现 blpop 阻塞了 redis 客户端的其他非阻塞方法,所以其他使用 redis 的函数将永远等待。事件循环仍在运行。

最佳答案

正如@AnonyMouze 提到的,执行client.duplicate().blpop(...) 是释放连接的技巧。

关于node.js - Nodejs redis 方法 BLPOP 阻塞事件循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63165982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com