libb.RateLimitedExecutor
- class RateLimitedExecutor(max_workers=None, max_per_second=inf, show_progress=False)[source]
Bases:
objectThread pool executor with rate limiting and Request/Response API.
Provides clean request/response API where every response includes the original request for easy result tracking and exception handling.
Basic Usage:
with RateLimitedExecutor(max_workers=10, max_per_second=5, show_progress=True) as executor: responses = executor.execute_items(process_fn, items, desc='Processing') for response in responses: if response.success: print(f"Item {response.request.id}: {response.result}") else: print(f"Item {response.request.id} failed: {response.exception}")
Advanced Usage with Custom IDs:
requests = [TaskRequest(item=x, id=f'custom_{i}') for i, x in enumerate(items)] responses = executor.execute(process_fn, requests, desc='Processing') result_map = {r.request.id: r.result for r in responses if r.success}
- __init__(max_workers=None, max_per_second=inf, show_progress=False)[source]
Initialize rate-limited executor.
- execute(fn, requests, desc='Processing', unit='item')[source]
Execute function on all requests and return responses in order.
- Parameters:
- Returns:
List of TaskResponse objects in same order as requests.
- Return type:
- execute_items(fn, items, desc='Processing', unit='item')[source]
Execute function on items with auto-generated request IDs.