Eric Redis queues documentation#
A Redis implementation of persistence layer of eric-sse:
An example of microservice based on this project
Also here there is a simple flow for testing purposes
Reference#
Redis support for eric-sse: RedisStorage persists channels, connections, listeners and queues,
RedisQueue keeps messages. See eric_redis_queues.repository.redis_channel_repository_builder().
- PREFIX_STORAGE = 'eric-redis-queues:kv:'#
Default key prefix of
RedisStorage.
- PREFIX_MESSAGES = 'eric-redis-queues:m'#
Key prefix of
RedisQueuemessage lists.
- class RedisConnection#
Bases:
objectRedisConnection(host: str = ‘127.0.0.1’, port: int = 6379, db: int = 0)
- class RedisStorage#
Bases:
KvStorageKvStoragebacked by Redis. Values are pickled.- Parameters:
redis_connection – Defaults to a local Redis, database 0. Shares the client of queues with equal connection data.
prefix – Prepended to every key. Prefixes of storages sharing a database must not be prefixes of each other.
- __init__(redis_connection=None, prefix='eric-redis-queues:kv:')#
- Parameters:
redis_connection (RedisConnection | None)
prefix (str)
- fetch_by_prefix(prefix)#
Search by KV prefix. An empty prefix returns all items.
Implementations must tolerate deletions of fetched keys while the result is being iterated.
- upsert(key, value)#
Updates or inserts a value given its corresponding key
- class RedisQueue#
Bases:
QueueDurable queue keeping pickled messages in a Redis list. Pickling it saves only its connection data and id.
pop()without timeout returns immediately, with a timeout it waits with BLPOP.wait_pop()runs BLPOP on an async client, so it holds no thread, but holds a Redis connection while waiting. Queues with equal connection data share one client per event loop.- Parameters:
redis_connection – Defaults to a local Redis, database 0.
queue_id – Defaults to a new uuid.
- __init__(redis_connection=None, queue_id=None)#
- Parameters:
redis_connection (RedisConnection | None)
queue_id (str | None)
- pop(timeout=0)#
Next message from the queue.
- Parameters:
timeout (float) – Seconds to wait for a message. With 0 it returns immediately.
- Return type:
Raises a
NoMessagesExceptionif no message is available, after waiting timeout seconds.
- async wait_pop(timeout)#
Waits for the next message without blocking the event loop.
The default implementation runs
pop()with timeout in a worker thread, so it takes one thread per waiting call. Override it with a native async wait when possible.Raises a
NoMessagesExceptionif no message arrives within timeout.
- purge()#
Deletes all its messages.
- Return type:
None
- class RedisQueueRepository#
Bases:
KvQueueRepository[RedisQueue]Creates, persists and loads
RedisQueueinstances.Queues are stored as they are: only their identity (connection data and queue id) is serialised, their messages stay in Redis. Deleting a queue deletes its messages too, that would be left in Redis otherwise.
- Parameters:
redis_connection – Connection of the queues it creates.
storage – Storage for queues identity, keyed by connection id.
- __init__(redis_connection, storage)#
- Parameters:
redis_connection (RedisConnection)
storage (KvStorage)
- redis_channel_repository_builder(channel_class, redis_connection=None, prefix=None)#
Returns a
ChannelRepositoryBuilderpersisting everything in Redis:repository = redis_channel_repository_builder(SSEChannel, RedisConnection(host='localhost')).build()
It can be further configured before building, e.g. with listeners or error handlers.
- Parameters:
channel_class (type[C]) – e.g.
SSEChannel.redis_connection (RedisConnection | None) – Used by queues, and by the storage of everything else. Defaults to a local Redis, database 0.
prefix (str | None) – Key prefix of the storage, see
RedisStorage.
- Return type: