如何重定向redis订阅的输出 [英] How to redirect the output of redis subscription
问题描述
我正在探索 redis 以进行发布/订阅.我想编写一个脚本,使用 redis-cli 订阅频道并将发布的任何内容转储到文件中.然而,我注意到的是 redis-cli 订阅频道 > 输出不太工作.
I am exploring redis to do pub/sub. I wanted to write a script that uses redis-cli to subscribe to a channel and dump whatever is published to a file. What I notice however is that redis-cli subscripe channel > output does not quite work.
非常感谢您的帮助.
问候,卡夏普
推荐答案
这是因为当 redis-cli 显示与订阅关联的消息时,没有自动刷新 stdout.所以停止redis-cli之前的最后一条消息不会出现在输出文件中.
This is because there is no automatic flush of stdout when redis-cli displays the messages associated to the subscription. So the last messages before stopping redis-cli do not appear in the output file.
没有选项可用于强制执行系统刷新,需要修补 redis-cli.c.在Redis源码中,编辑src/redis-cli.c,找到如下一段代码.添加缺少的 fflush 行.
There is no option you can use to enforce a systematic flush, redis-cli.c needs to be patched. In Redis source code, edit src/redis-cli.c, and find the following piece of code. Add the missing fflush line.
if (config.pubsub_mode) {
if (config.output != OUTPUT_RAW)
printf("Reading messages... (press Ctrl-C to quit)\n");
while (1) {
if (cliReadReply(output_raw) != REDIS_OK) exit(1);
// The following line must be added
fflush(stdout);
}
}
重新编译 redis-cli 后,它应该可以正常工作.
Once redis-cli has been compiled again, it should work as expected.
这篇关于如何重定向redis订阅的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!