Returns an iterator equivalent to map(fn, iter). Automatically propagates the current thread's context to worker threads.
(
self,
fn: Callable[..., T],
*iterables: Any,
timeout: float | None = None,
chunksize: int = 1,
)
| 277 | return super().submit(wrapper, *args, **kwargs) |
| 278 | |
| 279 | def map( |
| 280 | self, |
| 281 | fn: Callable[..., T], |
| 282 | *iterables: Any, |
| 283 | timeout: float | None = None, |
| 284 | chunksize: int = 1, |
| 285 | ) -> Any: |
| 286 | """ |
| 287 | Returns an iterator equivalent to map(fn, iter). |
| 288 | Automatically propagates the current thread's context to worker threads. |
| 289 | """ |
| 290 | main_trace_id = get_current_trace_id() |
| 291 | main_api_path = get_current_api_path() |
| 292 | main_env = get_current_env() |
| 293 | main_user_type = get_current_user_type() |
| 294 | main_user_name = get_current_user_name() |
| 295 | main_context = get_current_context() |
| 296 | |
| 297 | @functools.wraps(fn) |
| 298 | def wrapper(*args: Any, **kwargs: Any) -> Any: |
| 299 | if main_context: |
| 300 | # Create and set new context in worker thread |
| 301 | child_context = RequestContext( |
| 302 | trace_id=main_trace_id, |
| 303 | api_path=main_api_path, |
| 304 | env=main_env, |
| 305 | user_type=main_user_type, |
| 306 | user_name=main_user_name, |
| 307 | ) |
| 308 | child_context._data = main_context._data.copy() |
| 309 | set_request_context(child_context) |
| 310 | |
| 311 | return fn(*args, **kwargs) |
| 312 | |
| 313 | return super().map(wrapper, *iterables, timeout=timeout, chunksize=chunksize) |
| 314 | |
| 315 | |
| 316 | # Type for trace_id getter function |