Eric Redis queues documentation#

A Redis implementation of persistence layer of eric-sse:

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 RedisQueue message lists.

class RedisConnection#

Bases: object

RedisConnection(host: str = ‘127.0.0.1’, port: int = 6379, db: int = 0)

__init__(host='127.0.0.1', port=6379, db=0)#
Parameters:
Return type:

None

class RedisStorage#

Bases: KvStorage

KvStorage backed 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:
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.

Parameters:

prefix (str)

Return type:

Iterable[Any]

upsert(key, value)#

Updates or inserts a value given its corresponding key

Parameters:
fetch_one(key)#

Return value correspondant to key

Parameters:

key (str)

Return type:

Any

delete(key)#

Idempotent deletion. Do not throw an error on invalid key

Parameters:

key (str)

class RedisQueue#

Bases: Queue

Durable 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:
pop(timeout=0)#

Next message from the queue.

Parameters:

timeout (float) – Seconds to wait for a message. With 0 it returns immediately.

Return type:

Any

Raises a NoMessagesException if 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.

Parameters:

timeout (float) – Seconds to wait for a message.

Return type:

Any

Raises a NoMessagesException if no message arrives within timeout.

purge()#

Deletes all its messages.

Return type:

None

class RedisQueueRepository#

Bases: KvQueueRepository[RedisQueue]

Creates, persists and loads RedisQueue instances.

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:
delete(connection_id)#

Deletes a queue given the connection id it belongs to.

Parameters:

connection_id (str)

redis_channel_repository_builder(channel_class, redis_connection=None, prefix=None)#

Returns a ChannelRepositoryBuilder persisting 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:

ChannelRepositoryBuilder[C]