Skip to content

Exception raised when publishing from a pubsub object #151

nfarring opened this Issue Jul 2, 2011 · 2 comments

3 participants

nfarring commented Jul 2, 2011

pip freeze | grep redis


This code works correctly:

import redis
r = redis.Redis(password='yomama')

This code fails, although the message is correctly published before the code raises an exception:

import redis
r = redis.Redis(password='yomama')
ps = r.pubsub()

Traceback (most recent call last):
File "./test2", line 6, in
File "/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/", line 1136, in publish
return self.execute_command('PUBLISH', channel, message)
File "/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/", line 1063, in execute_command
return self.parse_response()
File "/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/", line 1078, in parse_response
if response[0] in self.subscribe_commands:
TypeError: 'long' object is not subscriptable

Here is the same output using ipython:

TypeError Traceback (most recent call last)

/Users/nfarring/ in ()

/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/client.pyc in publish(self, channel, message)
1134 Returns the number of subscribers the message was delivered to.
1135 """
-> 1136 return self.execute_command('PUBLISH', channel, message)
1138 def listen(self):

/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/client.pyc in execute_command(self, args, *kwargs)
1061 try:
1062 connection.send_command(*args)
-> 1063 return self.parse_response()
1064 except ConnectionError:
1065 connection.disconnect()

/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/client.pyc in parse_response(self)
1076 "Parse the response from a publish/subscribe command"
1077 response = self.connection.read_response()
-> 1078 if response[0] in self.subscribe_commands:
1079 self.subscription_count = response[2]
1080 # if we've just unsubscribed from the remaining channels,

TypeError: 'long' object is not subscriptable


It was an error to add the publish command to the PubSub object. While the error you describe above is a failure on the Python side, Redis actually prevents publishing to any connection that's currently [P]SUBSCRIBEd to one or more channels. Therefore, having publish on PubSub objects isn't very useful. Instead, you should use publish on the base Redis class to send messages.

I've already removed publish from the PubSub class in master which will become 2.4.6 within the next day or two.

Thanks for the bug report!

saidimu commented Nov 30, 2012

According to the Redis docs on SUBSCRIBE:

Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands.

The above commands are allowed by Redis but the Python client doesn't allow any command at all after listen() (the equivalent of entering the 'subscribe state').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.