FastAPI Event Emitter¶
In Pyventus, you can easily integrate Event Emitters with the FastAPI framework through the FastAPI Processing Service. Simply create an instance of the FastAPI Processing Service and pass it as the event processor when setting up the Event Emitter, or you can use the factory method called FastAPI Event Emitter to handle the setup in a single step.
By utilizing the FastAPI Processing Service, the execution of each event emission will be handled by the FastAPI's Background Tasks system.
Practical Example¶
To start using the Event Emitter with FastAPI, follow these steps:
-
Install Dependencies: Before proceeding, ensure that you have installed the optional FastAPI dependency.
-
Subscribe and Emit: Once you have everything installed and configured, using Pyventus with FastAPI is straightforward. Simply define your subscribers and events, instantiate an Event Emitter configured for FastAPI, and emit your events.
To start the FastAPI server, run the following command:
Open your browser and navigate to http://127.0.0.1:8000/email?email_to=email@email.com. You should see the following JSON response:
Additionally, you will be able to view the output of the functions in the console logs.
INFO Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO Started reloader process [12604] using WatchFiles INFO Started server process [7008] INFO Waiting for application startup. INFO Application startup complete. INFO 127.0.0.1:49763 - "GET /email?email_to=email@email.com HTTP/1.1" 200 Sending email to: email@email.com Email sent successfully!