Asynchronous Processing
Relevant source files
The following files were used as context for generating this wiki page:
OdooConnectorAPI utilizes asynchronous processing to facilitate efficient communication between external applications and the Odoo ERP system. By leveraging the FastAPI framework, the application handles incoming RESTful requests non-blockingly, which is essential for maintaining responsiveness when performing I/O-bound operations such as database queries or remote procedure calls (RPC) to an Odoo instance.
The system is designed to be containerized and scalable, utilizing Uvicorn as an ASGI server to manage asynchronous workers. This architecture allows the API to process multiple concurrent connections, fetching data from Odoo’s res.partner model or other entities without blocking the main execution thread.
Sources: README.md:5-9, app/main.py:1-6, requirements.txt:1-7
Asynchronous Architecture and Flow
The application architecture relies on the async/await pattern provided by Python and FastAPI. When a client performs a request, the ASGI server (Uvicorn) delegates the task to an asynchronous handler. Even when interacting with external libraries like odoorpc—which may perform synchronous network calls—the API endpoints are defined as async to allow the FastAPI event loop to manage context switching effectively.
Request Lifecycle Flow
The following diagram illustrates the flow of an asynchronous request through the API to the Odoo backend.
This sequence demonstrates how the FastAPI endpoint orchestrates the connection, authentication, and data retrieval processes.
Sources: app/main.py:22-38, odoo/test_odoo_connection.py:6-15
Key Components for Asynchronous Execution
The asynchronous capabilities are supported by several configuration layers, from the Python requirements to the deployment environment.
1. ASGI Server (Uvicorn)
Uvicorn serves as the lightning-fast ASGI server implementation. In both local development and Docker environments, it is configured to run the FastAPI application with auto-reload capabilities for development efficiency.
Sources: README.md:38, docker-compose.yml:6, requirements.txt:7
2. FastAPI Endpoints
Endpoints are defined using the async def syntax. This allows the application to yield control back to the event loop while waiting for external resources.
| Endpoint | Method | Description | Implementation Detail |
|---|---|---|---|
/ | GET | Health check/Root | Returns a welcome message. |
/get/partners | GET | Data Retrieval | Connects to Odoo via RPC to fetch partner list. |
Sources: app/main.py:17-38
3. Environment Configuration
Asynchronous tasks depend on correctly configured environment variables to establish connections to the Odoo backend. These are injected via Docker Compose or Kubernetes Helm charts.
# Example Environment Configuration from Helm values.yaml
env:
ODOO_URL: "localhost"
ODOO_DB: "mydb"
ODOO_USERNAME: "admin"
ODOO_PASSWORD: "admin"
Sources: charts/odooconnectorapi/values.yaml:14-18, app/main.py:9-14
Data Processing Logic
The core logic for processing Odoo data involves establishing a session, searching for records, and reading specific fields. Although the odoorpc library is used for the connection, the FastAPI wrapper ensures these interactions are exposed as asynchronous web services.
Logic Flow for Data Retrieval
The get_partners function specifically initializes an empty list and populates it only if the Odoo environment (odoo.env) is successfully accessed, ensuring robust handling of the asynchronous data flow.
Sources: app/main.py:27-37, odoo/test_odoo_connection.py:8-12
Infrastructure and Scaling
For production environments, the application is deployed using Kubernetes, which manages the lifecycle of the asynchronous workers. The Helm chart allows for scaling the number of replicas, enabling the system to handle higher volumes of asynchronous traffic.
- Replicas: Managed via
replicaCountinvalues.yaml. - Containerization: Uses a Dockerfile built via
docker-compose.ymlto package the FastAPI app and its dependencies.
Sources: charts/odooconnectorapi/values.yaml:20, docker-compose.yml:4-5, README.md:41-55
Conclusion
Asynchronous processing in the OdooConnectorAPI is achieved by wrapping Odoo RPC calls within FastAPI's asynchronous request handlers. By utilizing Uvicorn and containerized deployments, the project ensures that communication with the Odoo ERP remains efficient and scalable, providing a non-blocking interface for external applications to consume Odoo data.