Asynchronous Usage
Using DataBridge asynchronously with Python
Asynchronous Usage
DataBridge provides full asynchronous support through the AsyncDataBridge
class. This guide demonstrates how to use DataBridge in asynchronous applications.
Getting Started
Import and initialize the async client:
Basic Operations
The async API mirrors the synchronous API but uses async/await syntax:
Parallel Operations
One of the key benefits of async is the ability to run operations in parallel:
Batched Document Processing
Process multiple documents efficiently:
Async Cache Operations
Working with caches asynchronously:
Integration with Web Frameworks
FastAPI Example
Best Practices
-
Always Close the Client: Use async context managers or explicitly call
await db.close()
to ensure proper cleanup. -
Control Concurrency: Use semaphores to limit concurrent operations to avoid overwhelming resources.
-
Error Handling: Properly handle exceptions in async code to prevent unhandled promise rejections.
- Cancel Long Operations: Implement cancellation for long-running operations.
- Use Task Groups: For Python 3.11+, use task groups for cleaner async code.