Parallel Loading¶
parallel ¶
Enhanced parallel API utilities for T3 API client.
This module provides enhanced parallel loading capabilities that work with our httpx-based T3APIClient, including async support, rate limiting, and batching features.
RateLimiter ¶
Simple rate limiter using a token-bucket-style algorithm.
Enforces a minimum time interval between consecutive requests to avoid
overwhelming the API. The limiter tracks the timestamp of the last
request and sleeps for the remaining interval before allowing the next
one. Both synchronous (time.sleep) and asynchronous
(asyncio.sleep) acquisition methods are provided.
Attributes:
| Name | Type | Description |
|---|---|---|
requests_per_second |
Configured maximum throughput. |
|
min_interval |
Minimum seconds between consecutive requests,
derived as |
|
last_request_time |
Epoch timestamp of the most recent request. |
Initialize rate limiter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requests_per_second
|
float
|
Maximum requests per second allowed.
A value of |
10.0
|
Source code in t3api_utils/api/parallel.py
acquire ¶
Block synchronously until it's safe to make another request.
Uses time.sleep to pause the calling thread when the elapsed
time since the last request is less than min_interval.
Source code in t3api_utils/api/parallel.py
acquire_async
async
¶
Asynchronously wait until it's safe to make another request.
Uses asyncio.sleep instead of time.sleep so that the
event loop remains unblocked while waiting for the rate-limit
interval to elapse.
Source code in t3api_utils/api/parallel.py
parallel_load_paginated_sync ¶
parallel_load_paginated_sync(client: T3APIClient, path: str, max_workers: Optional[int] = None, rate_limit: Optional[float] = 10.0, **method_kwargs: Any) -> List[PaginatedT]
Load all pages of a paginated API endpoint in parallel (sync wrapper).
This is a wrapper around the async implementation using asyncio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
T3APIClient
|
Authenticated T3APIClient instance |
required |
path
|
str
|
API endpoint path (e.g., "/v2/licenses", "/v2/packages/active") |
required |
max_workers
|
Optional[int]
|
Maximum number of threads to use (maps to max_concurrent for async) |
None
|
rate_limit
|
Optional[float]
|
Requests per second limit (None to disable) |
10.0
|
**method_kwargs
|
Any
|
Arguments to pass to the API method |
{}
|
Returns:
| Type | Description |
|---|---|
List[PaginatedT]
|
List of paginated response objects, one per page |
Raises:
| Type | Description |
|---|---|
ValueError
|
If response is invalid |
AttributeError
|
If client is not authenticated |
Source code in t3api_utils/api/parallel.py
parallel_load_paginated_async
async
¶
parallel_load_paginated_async(client: T3APIClient, path: str, max_concurrent: Optional[int] = 10, rate_limit: Optional[float] = 10.0, batch_size: Optional[int] = None, **method_kwargs: Any) -> List[PaginatedT]
Load all pages of a paginated API endpoint in parallel using async client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
T3APIClient
|
Authenticated T3APIClient instance |
required |
path
|
str
|
API endpoint path (e.g., "/v2/licenses", "/v2/packages/active") |
required |
max_concurrent
|
Optional[int]
|
Maximum number of concurrent requests |
10
|
rate_limit
|
Optional[float]
|
Requests per second limit (None to disable) |
10.0
|
batch_size
|
Optional[int]
|
Process requests in batches of this size (None for no batching) |
None
|
**method_kwargs
|
Any
|
Arguments to pass to the API method |
{}
|
Returns:
| Type | Description |
|---|---|
List[PaginatedT]
|
List of paginated response objects, one per page |
Raises:
| Type | Description |
|---|---|
ValueError
|
If response is invalid |
AttributeError
|
If client is not authenticated |
Source code in t3api_utils/api/parallel.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
load_all_data_sync ¶
load_all_data_sync(client: T3APIClient, path: str, max_workers: Optional[int] = None, rate_limit: Optional[float] = 10.0, **method_kwargs: Any) -> List[T]
Load all data from a paginated endpoint and flatten into a single list (sync).
This is a wrapper around the async implementation using asyncio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
T3APIClient
|
Authenticated T3APIClient instance |
required |
path
|
str
|
API endpoint path (e.g., "/v2/licenses", "/v2/packages/active") |
required |
max_workers
|
Optional[int]
|
Maximum number of threads to use (maps to max_concurrent for async) |
None
|
rate_limit
|
Optional[float]
|
Requests per second limit (None to disable) |
10.0
|
**method_kwargs
|
Any
|
Arguments to pass to the API method |
{}
|
Returns:
| Type | Description |
|---|---|
List[T]
|
Flattened list of all data items across all pages |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the first response is missing required pagination
fields ( |
AttributeError
|
If the client is not authenticated. |
Source code in t3api_utils/api/parallel.py
load_all_data_async
async
¶
load_all_data_async(client: T3APIClient, path: str, max_concurrent: Optional[int] = 10, rate_limit: Optional[float] = 10.0, batch_size: Optional[int] = None, **method_kwargs: Any) -> List[T]
Load all data from a paginated endpoint and flatten into a single list (async).
This is a convenience function that combines parallel_load_paginated_async with data extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
T3APIClient
|
Authenticated T3APIClient instance |
required |
path
|
str
|
API endpoint path (e.g., "/v2/licenses", "/v2/packages/active") |
required |
max_concurrent
|
Optional[int]
|
Maximum number of concurrent requests |
10
|
rate_limit
|
Optional[float]
|
Requests per second limit (None to disable) |
10.0
|
batch_size
|
Optional[int]
|
Process requests in batches of this size (None for no batching) |
None
|
**method_kwargs
|
Any
|
Arguments to pass to the API method |
{}
|
Returns:
| Type | Description |
|---|---|
List[T]
|
Flattened list of all data items across all pages |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the first response is missing required pagination
fields ( |
AttributeError
|
If the client is not authenticated. |
Source code in t3api_utils/api/parallel.py
parallel_load_collection_enhanced ¶
parallel_load_collection_enhanced(method: Callable[..., PaginatedT], max_workers: Optional[int] = None, rate_limit: Optional[float] = None, **method_kwargs: Any) -> List[PaginatedT]
Enhanced version of the original parallel_load_collection with rate limiting.
This function maintains backwards compatibility with the original interface while adding rate limiting capabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable[..., PaginatedT]
|
Callable that returns a paginated response. Must accept a
|
required |
max_workers
|
Optional[int]
|
Maximum number of threads to use |
None
|
rate_limit
|
Optional[float]
|
Requests per second limit (None to disable) |
None
|
**method_kwargs
|
Any
|
Arguments to pass to the method |
{}
|
Returns:
| Type | Description |
|---|---|
List[PaginatedT]
|
List of paginated response objects |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the first response is missing a |
Source code in t3api_utils/api/parallel.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |