|
| 1 | +# AsyncZeroClient API Reference |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +`AsyncZeroClient` is the asynchronous client for calling RPC methods with async/await support. |
| 6 | + |
| 7 | +## Constructor |
| 8 | + |
| 9 | +```python |
| 10 | +AsyncZeroClient( |
| 11 | + host: str, |
| 12 | + port: int, |
| 13 | + default_timeout: int = 2000, |
| 14 | + encoder: Type[Encoder] = GenericEncoder, |
| 15 | + protocol: Type[AsyncZeroClientProtocol] = AsyncZMQClient, |
| 16 | + pool_size: int = 50, |
| 17 | +) |
| 18 | +``` |
| 19 | + |
| 20 | +### Parameters |
| 21 | + |
| 22 | +- **host** (str): Server hostname or IP address. |
| 23 | +- **port** (int): Server port number. |
| 24 | +- **default_timeout** (int): Default timeout for calls in milliseconds. Default: 2000 |
| 25 | +- **encoder** (Encoder): Message encoder matching server. Default: Msgspec |
| 26 | +- **protocol** (AsyncClientProtocol): Async communication protocol. Default: ZeroMQ |
| 27 | + - `zero.protocols.zeromq.AsyncZeroMQClient` - ZeroMQ (default) |
| 28 | + - `zero.protocols.tcp.AsyncTCPClient` - Raw TCP |
| 29 | +- **pool_size** (int): Connection pool size for reusing connections. Default: 50 |
| 30 | + |
| 31 | +### Example |
| 32 | + |
| 33 | +```python |
| 34 | +import asyncio |
| 35 | +from zero import AsyncZeroClient |
| 36 | +from zero.protocols.tcp import AsyncTCPClient |
| 37 | + |
| 38 | +async def main(): |
| 39 | + client = AsyncZeroClient( |
| 40 | + host="192.168.1.100", |
| 41 | + port=5559, |
| 42 | + protocol=AsyncTCPClient |
| 43 | + ) |
| 44 | + |
| 45 | +asyncio.run(main()) |
| 46 | +``` |
| 47 | + |
| 48 | +## Methods |
| 49 | + |
| 50 | +### call |
| 51 | + |
| 52 | +Asynchronously call an RPC method on the server. |
| 53 | + |
| 54 | +```python |
| 55 | +async def call( |
| 56 | + rpc_func_name: str, |
| 57 | + msg: AllowedType, |
| 58 | + timeout: Optional[int] = None, |
| 59 | + return_type: Optional[Type[T]] = None, |
| 60 | +) -> T |
| 61 | +``` |
| 62 | + |
| 63 | +**Parameters:** |
| 64 | + |
| 65 | +- **rpc_func_name** (str): Name of the RPC method to call |
| 66 | +- **msg** (Any): Argument to pass to the method |
| 67 | +- **timeout** (int): Timeout for the call in milliseconds. Optional. Defaults to `default_timeout`. |
| 68 | +- **return_type** (Type): Expected return type for type conversion. Optional. |
| 69 | + |
| 70 | +**Returns:** Response from the server (awaitable) |
| 71 | + |
| 72 | +**Raises:** Exception if server returns error |
| 73 | + |
| 74 | +## Usage Examples |
| 75 | + |
| 76 | +### Basic Async Call |
| 77 | + |
| 78 | +```python |
| 79 | +import asyncio |
| 80 | +from zero import AsyncZeroClient |
| 81 | + |
| 82 | +async def main(): |
| 83 | + client = AsyncZeroClient("localhost", 5559) |
| 84 | + result = await client.call("hello_world", None) |
| 85 | + print(result) |
| 86 | + |
| 87 | +asyncio.run(main()) |
| 88 | +``` |
| 89 | + |
| 90 | +### Multiple Sequential Calls |
| 91 | + |
| 92 | +```python |
| 93 | +import asyncio |
| 94 | +from zero import AsyncZeroClient |
| 95 | + |
| 96 | +async def main(): |
| 97 | + client = AsyncZeroClient("localhost", 5559) |
| 98 | + |
| 99 | + result1 = await client.call("method1", "data1") |
| 100 | + result2 = await client.call("method2", result1) |
| 101 | + result3 = await client.call("method3", result2) |
| 102 | + |
| 103 | + print(result3) |
| 104 | + |
| 105 | +asyncio.run(main()) |
| 106 | +``` |
| 107 | + |
| 108 | +### Concurrent Requests |
| 109 | + |
| 110 | +```python |
| 111 | +import asyncio |
| 112 | +from zero import AsyncZeroClient |
| 113 | + |
| 114 | +async def main(): |
| 115 | + client = AsyncZeroClient("localhost", 5559) |
| 116 | + |
| 117 | + # Run 10 requests concurrently |
| 118 | + results = await asyncio.gather( |
| 119 | + *[client.call("process", i) for i in range(10)] |
| 120 | + ) |
| 121 | + |
| 122 | + print(f"Completed {len(results)} requests") |
| 123 | + |
| 124 | +asyncio.run(main()) |
| 125 | +``` |
| 126 | + |
| 127 | +### With Type Conversion |
| 128 | + |
| 129 | +```python |
| 130 | +import asyncio |
| 131 | +from zero import AsyncZeroClient |
| 132 | +from dataclasses import dataclass |
| 133 | + |
| 134 | +@dataclass |
| 135 | +class User: |
| 136 | + id: int |
| 137 | + name: str |
| 138 | + email: str |
| 139 | + |
| 140 | +async def main(): |
| 141 | + client = AsyncZeroClient("localhost", 5559) |
| 142 | + |
| 143 | + user = await client.call("get_user", 1, return_type=User) |
| 144 | + print(f"User: {user.name} <{user.email}>") |
| 145 | + |
| 146 | +asyncio.run(main()) |
| 147 | +``` |
| 148 | + |
| 149 | +## Protocol Selection |
| 150 | + |
| 151 | +### ZeroMQ (Default) |
| 152 | + |
| 153 | +```python |
| 154 | +client = AsyncZeroClient("localhost", 5559) |
| 155 | +# Uses ZeroMQ by default |
| 156 | +``` |
| 157 | + |
| 158 | +### TCP (Better Performance) |
| 159 | + |
| 160 | +```python |
| 161 | +from zero.protocols.tcp import AsyncTCPClient |
| 162 | + |
| 163 | +client = AsyncZeroClient( |
| 164 | + "localhost", 5559, |
| 165 | + protocol=AsyncTCPClient |
| 166 | +) |
| 167 | +``` |
| 168 | + |
| 169 | +## Connection Pooling |
| 170 | + |
| 171 | +```python |
| 172 | +import asyncio |
| 173 | +from zero import AsyncZeroClient |
| 174 | + |
| 175 | +async def main(): |
| 176 | + # Single client handles multiple concurrent requests |
| 177 | + client = AsyncZeroClient("localhost", 5559) |
| 178 | + |
| 179 | + tasks = [ |
| 180 | + client.call("method", f"data_{i}") |
| 181 | + for i in range(100) |
| 182 | + ] |
| 183 | + |
| 184 | + results = await asyncio.gather(*tasks) |
| 185 | + print(f"Completed {len(results)} requests") |
| 186 | + |
| 187 | +asyncio.run(main()) |
| 188 | +``` |
| 189 | + |
| 190 | +## Best Practices |
| 191 | + |
| 192 | +✅ **DO:** |
| 193 | + |
| 194 | +- Use `asyncio.gather()` for concurrent requests |
| 195 | +- Reuse client instance across multiple calls |
| 196 | +- Use `asyncio.wait_for()` for timeouts |
| 197 | +- Handle `asyncio.TimeoutError` and `ConnectionError` |
| 198 | +- Use `return_type` for type conversion |
| 199 | + |
| 200 | +❌ **DON'T:** |
| 201 | + |
| 202 | +- Create new client for each call |
| 203 | +- Block the event loop with sync operations |
| 204 | +- Use sync `ZeroClient` in async code |
| 205 | + |
| 206 | +## Next Steps |
| 207 | + |
| 208 | +- [ZeroClient](client.md) - Synchronous client |
| 209 | +- [Guides - Async/Await](../guides/async-await.md) - Detailed async patterns |
| 210 | +- [Guides - Code Generation](../guides/code-generation.md) - Generate async clients |
0 commit comments