Skip to content

Commit f83c324

Browse files
bokelleyclaude
andcommitted
docs: add resource management section and context manager examples
Demonstrate the recommended async context manager pattern for proper connection cleanup. Update Quick Start example to use context manager, ensuring new users adopt best practices from the start. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b48b148 commit f83c324

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

README.md

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pip install adcp
2727
```python
2828
from adcp import ADCPMultiAgentClient, AgentConfig, GetProductsRequest
2929

30-
# Configure agents and handlers
31-
client = ADCPMultiAgentClient(
30+
# Configure agents and handlers (context manager ensures proper cleanup)
31+
async with ADCPMultiAgentClient(
3232
agents=[
3333
AgentConfig(
3434
id="agent_x",
@@ -54,21 +54,21 @@ client = ADCPMultiAgentClient(
5454
if metadata.status == "completed" else None
5555
)
5656
}
57-
)
58-
59-
# Execute operation - library handles operation IDs, webhook URLs, context management
60-
agent = client.agent("agent_x")
61-
request = GetProductsRequest(brief="Coffee brands")
62-
result = await agent.get_products(request)
57+
) as client:
58+
# Execute operation - library handles operation IDs, webhook URLs, context management
59+
agent = client.agent("agent_x")
60+
request = GetProductsRequest(brief="Coffee brands")
61+
result = await agent.get_products(request)
6362

64-
# Check result
65-
if result.status == "completed":
66-
# Agent completed synchronously!
67-
print(f"✅ Sync completion: {len(result.data.products)} products")
63+
# Check result
64+
if result.status == "completed":
65+
# Agent completed synchronously!
66+
print(f"✅ Sync completion: {len(result.data.products)} products")
6867

69-
if result.status == "submitted":
70-
# Agent will send webhook when complete
71-
print(f"⏳ Async - webhook registered at: {result.submitted.webhook_url}")
68+
if result.status == "submitted":
69+
# Agent will send webhook when complete
70+
print(f"⏳ Async - webhook registered at: {result.submitted.webhook_url}")
71+
# Connections automatically cleaned up here
7272
```
7373

7474
## Features
@@ -173,6 +173,32 @@ Or use the CLI:
173173
uvx adcp --debug myagent get_products '{"brief":"TV ads"}'
174174
```
175175

176+
### Resource Management
177+
178+
Use async context managers to ensure proper cleanup of connections:
179+
180+
```python
181+
# Recommended: Automatic cleanup with context manager
182+
async with ADCPClient(agent_config) as client:
183+
result = await client.get_products(request)
184+
# Connection automatically closed on exit
185+
186+
# Multi-agent client also supports context managers
187+
async with ADCPMultiAgentClient(agents) as client:
188+
results = await client.get_products(request)
189+
# All agent connections closed automatically
190+
```
191+
192+
Manual cleanup is also available if needed:
193+
194+
```python
195+
client = ADCPClient(agent_config)
196+
try:
197+
result = await client.get_products(request)
198+
finally:
199+
await client.close() # Explicit cleanup
200+
```
201+
176202
### Error Handling
177203

178204
The library provides a comprehensive exception hierarchy with helpful error messages:

0 commit comments

Comments
 (0)