MCPcopy
hub / github.com/anthropics/anthropic-sdk-python / asyncify

Function asyncify

src/anthropic/_utils/_sync.py:28–58  ·  view source on GitHub ↗

Take a blocking function and create an async one that receives the same positional and keyword arguments. Usage: ```python def blocking_func(arg1, arg2, kwarg1=None): # blocking code return result result = asyncify(blocking_function)(arg1, arg2, kwarg1=va

(function: Callable[T_ParamSpec, T_Retval])

Source from the content-addressed store, hash-verified

26
27# inspired by `asyncer`, https://github.com/tiangolo/asyncer
28def asyncify(function: Callable[T_ParamSpec, T_Retval]) -> Callable[T_ParamSpec, Awaitable[T_Retval]]:
29 """
30 Take a blocking function and create an async one that receives the same
31 positional and keyword arguments.
32
33 Usage:
34
35 ```python
36 def blocking_func(arg1, arg2, kwarg1=None):
37 # blocking code
38 return result
39
40
41 result = asyncify(blocking_function)(arg1, arg2, kwarg1=value1)
42 ```
43
44 ## Arguments
45
46 `function`: a blocking regular callable (e.g. a function)
47
48 ## Return
49
50 An async function that takes the same positional and keyword arguments as the
51 original one, that when called runs the same original function in a thread worker
52 and returns the result.
53 """
54
55 async def wrapper(*args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs) -> T_Retval:
56 return await to_thread(function, *args, **kwargs)
57
58 return wrapper

Callers 4

test_get_platformMethod · 0.90
requestMethod · 0.85
async_auth_flowMethod · 0.85
_ensure_access_tokenMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_get_platformMethod · 0.72