c - edis 中的 BRPOP 非阻塞

标签 c redis

我正在尝试使用 REDIS(与 edis 库)来同步一组生产者/工作人员。

工作人员会将其 ID(例如 123)发布到列表中,并使用 BRPOP 在名为 wrk:123 的列表上等待。我编写了下面的代码,但是当 LPUSH 成功时,BRPOP 返回空回复!

BRPOP 当然可以在 redis-cli 中完美运行。

我显然在这里做错了什么,但我不知道它是什么!!

      // Push my id to other clients
      reader = eredis_r( redis );
      sprintf(cmdbuf,"LPUSH readylist %d",myid);
      reply  = eredis_r_cmd(reader,cmdbuf);
      if (reply && reply->type == REDIS_REPLY_INTEGER) {
        printf("Reply: %lld\d",reply->integer);
      }

      // WAIT some other client to sendme data
      sprintf(cmdbuf,"BRPOP wrk:%d 0",myid);
      reply  = eredis_r_cmd(reader,cmdbuf);
      if (reply) {
        switch (reply->type) {
          case REDIS_REPLY_ERROR: printf("Reply: ERROR %.*s\n",(int)reply->len,reply->str);
                                  break;

                         default: printf("Reply type: %d\n",reply->type);
        }
      } 
      else printf("Reply is null\n");

      eredis_r_clear( reader );
      eredis_r_release( reader );

最佳答案

这似乎与以下hiredis问题有关: https://github.com/redis/hiredis/issues/722 这是由 BRPOP 行为的变化引起的:当 redis 连接超时设置为非 0 值时,该超时也由 BRPOP(和类似命令)继承。

将redis连接超时设置为0:

eredis_timeout(redis, 0);

使代码按预期运行。

关于c - edis 中的 BRPOP 非阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59589621/

相关文章:

c - 用 C 中的 uint64_t 数据填充 uint16_t 数组

C标准: L prefix and octal/hexadecimal escape sequences

对计算器程序的困惑

azure - 调整 Azure Function 内的 Redis 连接以防止超时

c - 链表元素在 for 循环中更新不佳

c - 在 C 中读取文件的最快方法

php - 如何查询laravel采集结果

python - Redis 和权限

redis - 何时使用键/值存储(例如 Redis)代替/与 SQL 数据库一起使用?

ruby-on-rails - 操作系统 : Rails Layer connect to Elasticache : Redis