Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576---title: async_utilssidebarTitle: async_utils---
# `fastmcp.utilities.async_utils`
Async utilities for FastMCP.
## Functions
### `is_coroutine_function` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/utilities/async_utils.py#L14" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonis_coroutine_function(fn: Any) -> bool```
Check if a callable is a coroutine function, unwrapping functools.partial.
``inspect.iscoroutinefunction`` returns ``False`` for``functools.partial`` objects wrapping an async function on Python < 3.12.This helper unwraps any layers of ``partial`` before checking.
### `call_sync_fn_in_threadpool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/utilities/async_utils.py#L26" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncall_sync_fn_in_threadpool(fn: Callable[..., Any], *args: Any, **kwargs: Any) -> Any```
Call a sync function in a threadpool to avoid blocking the event loop.
Uses anyio.to_thread.run_sync which properly propagates contextvars,making this safe for functions that depend on context (like dependency injection).
### `gather` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/utilities/async_utils.py#L53" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythongather(awaitables: Iterable[Awaitable[T]]) -> list[T] | list[T | BaseException]```
Run awaitables concurrently and return results in order.
Uses anyio TaskGroup for structured concurrency.
``awaitables`` is consumed lazily, one item at a time, right before eachis handed to the task group. Callers with a dynamic number of awaitablesshould pass a generator expression (e.g. ``gather(f(x) for x in xs)``)rather than a list or list comprehension: a list comprehension callsevery ``f(x)`` up front, creating a batch of coroutine objects beforethis function even starts, whereas a generator expression creates eachcoroutine only as this function's own scheduling loop asks for it. Thatmatters because coroutine creation and scheduling can be interruptedbetween any two bytecode instructions by a synchronous signal handler(for example pytest-timeout's SIGALRM-based per-test timeout). If thathappens while a whole batch of coroutines is sitting unscheduled, theyare silently abandoned and eventually trigger a "coroutine was neverawaited" warning attributed to whatever unrelated code happens to berunning when the garbage collector gets to them. Lazy consumption keepsthe window in which a created-but-unscheduled coroutine can exist assmall as possible.
**Args:**- `awaitables`: Iterable of awaitables to run concurrently.- `return_exceptions`: If True, exceptions are returned in results. If False, first exception cancels all and raises.
**Returns:**- List of results in the same order as input awaitables.