Bulk Operations (Python)
Since the Python SDK is async-native, you can use asyncio.gather or asyncio.TaskGroup to perform operations concurrently for high throughput.
Concurrent Tracking
Here is an example of tracking multiple events simultaneously.
import asyncio
from cdp_client.client import CDPClient
async def track_many(client: CDPClient, events: list):
"""
Tracks a list of events concurrently.
Each event in the list should be a dict: {'user_id': str, 'name': str, 'properties': dict}
"""
tasks = []
for event in events:
tasks.append(
client.track(event['user_id'], event['name'], event['properties'])
)
# Run all tasks concurrently and wait for completion
results = await asyncio.gather(*tasks, return_exceptions=True)
# Check for failures
for i, res in enumerate(results):
if isinstance(res, Exception):
print(f"Failed to track event {i}: {res}")
# Usage
# asyncio.run(track_many(client, my_events_list))
Best Practices
- Concurrency Limits: While
asyncio.gatheris powerful, avoid launching thousands of requests at once without a semaphore to prevent hitting rate limits or exhausting file handles. - Error Handling: Use
return_exceptions=Trueingatherso that one failed request doesn't stop the entire batch. - Client Reuse: Always reuse the same
CDPClientinstance; creating a new one for every request is inefficient.