Redis发布 - 'set'参数数量错误

2 投票
2 回答
5504 浏览
提问于 2025-04-18 02:03

我正在尝试在我的应用程序中使用Django的websockets功能,主要是为了实现一些小功能。

我在试着使用第一个示例,想通过django-websocket-redis来广播一条消息。

from ws4redis.publisher import RedisPublisher
redis_publisher = RedisPublisher(facility='foobar', broadcast=True)
redis_publisher.publish_message('Hello World')

我实际上已经能在订阅的客户端收到消息了,但我遇到了这个错误:

‘set’命令的参数数量不对 [...] 异常位置 my_virtualenv/local/lib/python2.7/site-packages/redis/connection.py 的 read_response,行 344

(这个错误是从publish_message()调用中追踪到的)

我的版本信息:

Django==1.6.2
django-websocket-redis==0.4.0
redis==2.9.1

有人能帮我调试一下这个问题吗?

2 个回答

2

看起来这是个bug。

解决办法:

ws4redis.redis_store.RedisStorepublish_message 函数里,把

self._connection.set(channel, message, ex=expire)

改成

self._connection.setex(channel, expire, message)

因为redis的 SET 命令不接受第三个参数。我猜原本是想设置一个在一定时间后过期的值,这个应该用redis的 SETEX 命令。py-redis 的 setex 方法是这样调用的:setex(name, time, value)

这样就能解决“'set'的参数数量错误”的问题。

参考链接: https://github.com/jrief/django-websocket-redis/pull/30

1

我最后把过期时间设置为0,作为一种解决办法。

WS4REDIS_EXPIRE = 0

这样可以防止ws4redis在redis里存储任何东西。


这个问题在 0.4.1 版本中已经修复了。

撰写回答