RQ Event Emitter¶
🏗️ Work in Progress
This section is currently being rebuilt.
In the previous sections, we explored different event emitters, such as AsyncIOEventEmitter
and
ExecutorEventEmitter
. Now, let's dive into the RQEventEmitter
, a powerful tool for handling events that
involve intensive asynchronous background tasks.
What is it?¶
The RQEventEmitter
is a concrete implementation of the EventEmitter
that takes advantage of the
Redis Queue
pub/sub and worker
system to execute event emissions.
This event emitter is particularly useful when dealing with events that require resource-intensive tasks like model optimization or video processing. By leveraging RQ workers, it enables non-blocking execution and enhances performance.
How it Works¶
The RQEventEmitter
seamlessly handles the emission and processing of events by utilizing the
RQ package. Here's how it works:
- Event emission: When an event is emitted, all associated event handlers are bundled into an EventEmission object, which is then enqueued into the Redis Queue system.
- Workers processing: The enqueued event emission object is asynchronously processed by available Python RQ workers, enabling efficient parallel execution.
Usage¶
To start using the RQEventEmitter
, follow these steps:
- Install Python RQ: Before proceeding, make sure you have installed the Redis Queue (RQ) optional dependency.
- Python RQ worker configuration:
The Python RQ workers act as processors for event emission objects. They listen to the
Redis Queue
pub/sub channel and process tasks when enqueued. To configure the workers, create a file namedworker.py
and include the worker configuration code. You can refer to the official RQ documentation for more advanced configurations. - Define event handlers:
After defining the worker file, let's focus on the event handlers. According to the RQ documentation, these
functions should not reside in the main module. Therefore, we need to create another module where all our event
handlers can be placed. For this example, let's create a file called
event_handlers.py
and add the handlers to be processed. - Emitting events:
Once the previous steps have been completed, the RQ workers can be started by running the
worker.py
script. Following that, amain.py
file should be created to instantiate anRQEventEmitter
and configure theQueue
where the event emission objects will be enqueued. For full details on the configuration options, please refer to the RQ website and documentation on the enqueue method settings. At this point, we are ready to emit our first event using theRQEventEmitter
.
Recap¶
We've seen how the RQEventEmitter
provides an asynchronous approach to event handling using
Redis Queues
and RQ workers. The main points are:
-
It leverages existing
Redis Queue
infrastructure for asynchronous task processing. - Event emissions are enqueued in Redis, and workers independently process them.
- This distributed model scales efficiently regardless of workload volume.