Process multiple code executions concurrently using async Python SDK.
pip install cognitora[async]
1import asyncio
2from cognitora import CognitoraAsync
3
4async def process_data_batch():
5 async with CognitoraAsync(api_key="your_api_key") as client:
6 # Define multiple processing tasks
7 tasks = [
8 "import pandas as pd; df = pd.read_csv('data1.csv'); print(df.mean())",
9 "import numpy as np; arr = np.random.rand(1000); print(np.std(arr))",
10 "import matplotlib.pyplot as plt; plt.plot([1,2,3]); plt.savefig('plot.png')",
11 "import requests; resp = requests.get('https://api.github.com'); print(resp.status_code)"
12 ]
13
14 # Execute all tasks concurrently
15 coroutines = [
16 client.code_interpreter.execute(
17 code=task,
18 language="python",
19 timeout_seconds=30
20 )
21 for task in tasks
22 ]
23
24 results = await asyncio.gather(*coroutines, return_exceptions=True)
25
26 # Process results
27 for i, result in enumerate(results):
28 if isinstance(result, Exception):
29 print(f"Task {i+1} failed: {result}")
30 else:
31 print(f"Task {i+1} completed: {result.data.status}")
32 for output in result.data.outputs:
33 if output.type == "stdout":
34 print(f" Output: {output.data}")
35
36# Run the batch processing
37asyncio.run(process_data_batch())
Get started with Async Batch Processing and Cognitora in minutes. Secure, scalable, and ready for anything.