Skip to content

Unable to receive events via psubscribe #3680

Closed
@smac89

Description

@smac89

Using the following code:

import asyncio
import os

from redis.asyncio import Redis

redis = Redis.from_url(f"redis://{os.getenv('APP_BACKEND_REDIS_HOST')}:{os.getenv('APP_BACKEND_REDIS_PORT')}")

async def on_redis_event(message: dict):
    """Handle Redis pubsub events."""
    print(f"Received Redis event: {message}")

async def redis_main():
    await redis.config_set("notify-keyspace-events", "KEA")
    # https://partnerpens.hashnode.dev/handling-cache-expiry-notifications-in-a-redis-based-application
    pubsub = redis.pubsub()
    await pubsub.connect()
    await pubsub.psubscribe(**{"__keyspace@0__:*": on_redis_event})

    await redis.set("hello", "world", 30)
    await asyncio.sleep(60)

if __name__ == '__main__':
    asyncio.run(redis_main())

When I run it, I don't get any output. The on_redis_event listener is never called. What could I be doing wrong?

version: redis[hiredis]==6.2.0

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