非阻塞Redis pubsub是否可能? [英] Is non-blocking Redis pubsub possible?

查看:25
本文介绍了非阻塞Redis pubsub是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用redis的pubsub传输一些消息,又不想被listen屏蔽,如下代码:

I want to use redis' pubsub to transmit some messages, but don't want be blocked using listen, like the code below:

import redis
rc = redis.Redis()

ps = rc.pubsub()
ps.subscribe(['foo', 'bar'])

rc.publish('foo', 'hello world')

for item in ps.listen():
    if item['type'] == 'message':
        print item['channel']
        print item['data']

最后一个 for 部分将被阻止.我只想检查给定的频道是否有数据,我该如何完成?有check之类的方法吗?

The last for section will block. I just want to check if a given channel has data, how can I accomplish this? Is there a check like method?

推荐答案

我认为这不可能.频道没有任何当前数据",您订阅频道并开始接收频道上其他客户端推送的消息,因此它是一个阻塞 API.此外,如果您查看 Redis 命令文档 发布/订阅,它会更清楚.

I don't think that would be possible. A Channel doesn't have any "current data", you subscribe to a channel and start receiving messages that are being pushed by other clients on the channel, hence it is a blocking API. Also if you look at the Redis Commands documentation for pub/sub it would make it more clear.

这篇关于非阻塞Redis pubsub是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆