gpt4 book ai didi

python - Tornado -redis : Why the 'listen' and the 'brpop' of tornado-redis can't work at the same time

转载 作者:IT王子 更新时间:2023-10-29 06:07:43 24 4
gpt4 key购买 nike

代码如下:

from tornadoredis import Client
from tornado.ioloop import IOLoop
from tornado.gen import coroutine, Task

rds = Client()

@coroutine
def listen_pub():
def handle(msg):
print msg

yield Task(rds.subscribe, channels='pub')
rds.listen(handle)


@coroutine
def listen_list():
while True:
res = yield Task(rds.brpop, keys='list')
print res


def test():
listen_pub()
listen_list()

test()
IOLoop.current().start()

当我运行上面的代码时,只有“listen_list”可以接收消息。为什么“listen_list”不起作用?如何同时收听来自 LIST 和 PUB/SUB 的消息?

最佳答案

看一下redis文档:

A client subscribed to one or more channels should not issue commands, although it can subscribe and unsubscribe to and from other channels. The reply of the SUBSCRIBE and UNSUBSCRIBE operations are sent in the form of messages, so that the client can just read a coherent stream of messages where the first element indicates the type of message.

你必须使用两个连接客户端。
来源:http://redis.io/topics/pubsub

关于python - Tornado -redis : Why the 'listen' and the 'brpop' of tornado-redis can't work at the same time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31559736/

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