Redis Event Emitter¶
In Pyventus, you can easily integrate Event Emitters with the Redis Queue framework through the Redis Processing Service. Simply create an instance of the Redis Processing Service and pass it as the event processor when setting up the Event Emitter, or you can use the factory method called Redis Event Emitter to handle the setup in a single step.
By utilizing the Redis Processing Service, the execution of each event emission will be handled by a Redis Queue worker.
Practical Example¶
To start using the Event Emitter with Redis Queue, follow these steps:
-
Install Dependencies: Before proceeding, ensure that you have installed the optional Redis Queue dependency.
-
Define Subscribers: If you're using Python's built-in functions, you can skip this step. If you're working with your own functions, you'll need to let Redis Queue know where they are defined. However, to avoid circular dependencies between modules, it's important to place these functions in a separate module from both your worker module and the event emitter.
-
Create a Worker: Now that you’ve defined your subscribers, the next step is to create the script for the Redis Queue worker. This worker will listen to the Redis Queue pub/sub channel and process each event emission. For more information about Redis Queue workers, you can refer to the official documentation: RQ Workers.
- Import the
subscribers.py
module to let Redis Queue know about the available functions. - Creates a new Worker instance and starts the work loop.
- Set the number of workers. For auto-assignment use:
multiprocessing.cpu_count()
. - Creates and starts new Processes for each worker.
- Join every worker process.
- Import the
subscribers.py
module to let Redis Queue know about the available functions. - Creates a new Worker instance and starts the work loop.
- A class that inherits from
SimpleWorker
and is used to create a new worker instance in a Windows based system. - Set the number of workers. For auto-assignment use:
multiprocessing.cpu_count()
. - Creates and starts new Processes for each worker.
- Join every worker process.
With the previous configuration in place, you can now launch the Redis Queue worker.
- Import the
-
Emitting events: Now that your workers are up and running, it’s time to start emitting events! Just create an Event Emitter configured with the Redis Processing Service, and you’re all set to emit an event.