pynenc.broker.redis_broker¶
Module Contents¶
Classes¶
RedisQueue: A helper class for managing message queues in Redis. |
|
A Redis-backed implementation of the BaseBroker. |
API¶
- class pynenc.broker.redis_broker.RedisQueue(app: pynenc.app.Pynenc, client: redis.Redis, name: str, namespace: str = 'queue')[source]¶
RedisQueue: A helper class for managing message queues in Redis.
This class provides methods for sending, receiving, and purging messages in a Redis queue. It is designed to work specifically with the RedisBroker class to handle message queuing.
- Parameters:
Initialization
- send_message(message: str) None[source]¶
Send a message to the Redis queue.
This method appends a message to the right end of the Redis list, effectively queuing it for processing.
- Parameters:
message (str) – The message to be queued.
- send_messages_batch(messages: list[str]) None[source]¶
Send multiple messages to the Redis queue in a single operation.
This method uses a Redis pipeline to send multiple messages to the queue in a single operation, which can improve performance when routing multiple invocations.
- receive_message() Optional[str][source]¶
Receive a message from the Redis queue.
This method blocks for a configured timeout waiting for a message. Uses BLPOP for efficient waiting instead of polling.
- Returns:
The message from the queue, or None if timeout is reached.
- class pynenc.broker.redis_broker.RedisBroker(app: pynenc.app.Pynenc)[source]¶
Bases:
pynenc.broker.base_broker.BaseBrokerA Redis-backed implementation of the BaseBroker.
This subclass of BaseBroker implements the abstract methods for routing, retrieving, and purging invocations using Redis as the message broker. It is suitable for production environments where robustness and scalability are required.
- Parameters:
app (Pynenc) – A reference to the Pynenc application.
Initialization
- property client: redis.Redis¶
Lazy initialization of Redis client
- property queue: pynenc.broker.redis_broker.RedisQueue¶
- route_invocation(invocation: pynenc.invocation.dist_invocation.DistributedInvocation) None[source]¶
Route an invocation by sending it to the Redis queue.
This method serializes the DistributedInvocation object to JSON and sends it to the Redis queue for processing using the
send_messagemethod.- Parameters:
invocation (DistributedInvocation) – The invocation to be queued.
- route_invocations(invocations: list[pynenc.invocation.dist_invocation.DistributedInvocation]) None[source]¶
Routes multiple invocations at once using Redis pipeline for better performance.
- Parameters:
invocations (list[DistributedInvocation]) – The invocations to be routed.
- retrieve_invocation() Optional[pynenc.invocation.dist_invocation.DistributedInvocation][source]¶
Retrieve the next invocation from the Redis queue.
This method receives a message from the Redis queue using the
receive_messagemethod. If a message is received, it deserializes the JSON string back into a DistributedInvocation object.- Returns:
The next invocation from the queue, or None if the queue is empty.