Skip to main content

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​

  1. Concurrency Limits: While asyncio.gather is powerful, avoid launching thousands of requests at once without a semaphore to prevent hitting rate limits or exhausting file handles.
  2. Error Handling: Use return_exceptions=True in gather so that one failed request doesn't stop the entire batch.
  3. Client Reuse: Always reuse the same CDPClient instance; creating a new one for every request is inefficient.