Redis integration for taskiq
Maintained. Last release 2 months ago, commits in 3 of the last 26 weeks. Weaker on commit activity.
pip install taskiq-redisTaskIQ-Redis
Taskiq-redis is a plugin for taskiq that adds a new broker and result backend based on redis.
Installation
To use this project you must have installed core taskiq library:
pip install taskiq
This project can be installed using pip:
pip install taskiq-redis
Usage
Let's see the example with the redis broker and redis async result:
# broker.py
import asyncio
from taskiq_redis import RedisAsyncResultBackend, RedisStreamBroker
result_backend = RedisAsyncResultBackend(
redis_url="redis://localhost:6379",
)
# Or you can use PubSubBroker if you need broadcasting
# Or ListQueueBroker if you don't want acknowledges
broker = RedisStreamBroker(
url="redis://localhost:6379",
).with_result_backend(result_backend)
@broker.task
async def best_task_ever() -> None:
"""Solve all problems in the world."""
await asyncio.sleep(5.5)
print("All problems are solved!")
async def main():
task = await best_task_ever.kiq()
print(await task.wait_result())
if __name__ == "__main__":
asyncio.run(main())
Launch the workers:
taskiq worker broker:broker
Then run the main code:
python3 broker.py
Brokers
This package contains 6 broker implementations. 3 broker types:
- PubSub broker
- ListQueue broker
- Stream broker
Each of type is implemented for each redis architecture:
- Single node
- Cluster
- Sentinel
Here's a small breakdown of how they differ from eachother.
PubSub
By default on old redis versions PUBSUB was the way of making redis into a queue. But using PUBSUB means that all messages delivered to all subscribed consumers.
[!WARNING] This broker doesn't support acknowledgements. If during message processing Worker was suddenly killed the message is going to be lost.
ListQueue
This broker creates a list of messages at some key. Adding new tasks will be done
by appending them from the left side using lpush, and taking them from the right side using brpop.
[!WARNING] This broker doesn't support acknowledgements. If during message processing Worker was suddenly killed the message is going to be lost.
Stream
Stream brokers use redis stream type to store and fetch messages.