Skip to content

Exception raised when publishing from a pubsub object #151

Closed

Description

@nfarring

pip freeze | grep redis

redis==2.4.5

This code works correctly:

import redis
r = redis.Redis(password='yomama')
r.publish('foo','bar')

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()
ps.publish('foo','bar')

Traceback (most recent call last):
File "./test2", line 6, in
ps.publish('foo','bar')
File "/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/client.py", line 1136, in publish
return self.execute_command('PUBLISH', channel, message)
File "/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/client.py", line 1063, in execute_command
return self.parse_response()
File "/Users/nfarring/.virtualenvs/mordia/lib/python2.7/site-packages/redis/client.py", 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)
1137
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

Activity

  1. andymccurdy commented on Jul 11, 2011

    @andymccurdy
    Contributor

    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!

  2. saidimu commented on Nov 30, 2012

    @saidimu
    Contributor

    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').

  3. eyison commented on Jul 15, 2019

    @eyison

    when I listened,after a while,I can‘t get message any more

    pool = redis.ConnectionPool(host=self.__host, port=self.__port, password=self.__password, db=self.__db)
    redis_pool = redis.StrictRedis(connection_pool=pool)
    redis_conn = redis_pool.redis_connect()
    pub = redis_conn.pubsub()
    pub.subscribe('abc')
    for msg in pub.listen():
          print(msg)
    
  4. andymccurdy commented on Jul 15, 2019

    @andymccurdy
    Contributor

    @eyison This seems like a completely different issue. I suspect the TCP connection between the client and server was interrupted. I would suggest using one of the keepalive settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions