Redis Director
Do you want to distribute payloads between multiple Redis keys?
Here you go!
Installation
pip install redis-director
How to Use
Publisher's Side
from redis import Redis
from redis_director import Publisher, Subscriber, generic_set_handler
pub = Publisher(
redis=Redis(),
score_key="my_stuff:my_fancy_score",
queue_key="my_stuff:my_amazing_queue",
)
def custom_handler(subscriber: Subscriber, payload):
"""
You can make custom handlers
if you have some complex stuff to do.
"""
subscriber.add_score(-2)
subscriber.redis.lpush(
"super:secret:route:hehe",
payload,
)
pub.add_subscriber(
"route_1",
handler=generic_set_handler(
"route_1:queue",
-1,
),
default_score=100,
min_score=1,
max_score=100,
).add_subscriber(
"route_2",
handler=custom_handler,
default_score=100,
min_score=0,
max_score=1000,
)
pub.redis.sadd(pub.queue_key, *range(0, 1000))
pub.publish(100)
Subscriber's Side
You can also play with the scores from the subscriber's side!
from redis import Redis
from redis_director import Subscriber
sub = Subscriber.new(
redis=Redis(),
score_key="my_stuff:my_fancy_score",
score_member="bobs_amazing_queue",
default_score=100,
min_score=1,
max_score=100,
)
sub.add_score(10)
sub.set_score(500)
sub.reset_score()