Convert
convert
async_to_sync
async_to_sync(
func_or_awaitable: Callable[_P, Awaitable[_T]],
) -> Callable[_P, _T]
async_to_sync(
func_or_awaitable: Awaitable[_T],
) -> Callable[[], _T]
async_to_sync(
func_or_awaitable: (
Callable[..., Awaitable[_T]] | Awaitable[_T]
),
) -> Callable[..., _T]
async_to_sync(
func_or_awaitable: (
Callable[_P, Awaitable[_T]] | Awaitable[_T]
),
) -> Callable[_P, _T] | Callable[[], _T]
Convert an awaitable function or awaitable object to a synchronous function.
If used within an asynchronous context, attempts to use the same backend. Defaults to asyncio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable[_P, Awaitable[_T]] | Awaitable[_T]
|
An awaitable function or awaitable object. |
required |
Returns:
| Type | Description |
|---|---|
Callable[_P, _T] | Callable[[], _T]
|
A synchronous function. |
Example
Source code in src/async_wrapper/convert/_sync/main.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
sync_to_async
Convert a synchronous function to an asynchronous function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable[_P, _T]
|
The synchronous function to be converted. |
required |
Returns:
| Type | Description |
|---|---|
Callable[_P, Awaitable[_T]]
|
An asynchronous function |
Callable[_P, Awaitable[_T]]
|
that behaves equivalently to the input synchronous function. |
Examples:
Source code in src/async_wrapper/convert/_async.py
toggle_func
toggle_func(
func: (
Callable[_T" optional hover>_T, _T]
| Callable[_T" optional hover>_T, Coroutine[Any, Any, _T]]
),
) -> Callable[_P, _T] | Callable[_P, Awaitable[_T]]
Convert between synchronous and asynchronous functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable[_P, _T] | Callable[_P, Coroutine[Any, Any, _T]]
|
A function that can be either synchronous or asynchronous. |
required |
Returns:
| Type | Description |
|---|---|
Callable[_P, _T] | Callable[_P, Awaitable[_T]]
|
A function that matches the desired synchronicity, |
Callable[_P, _T] | Callable[_P, Awaitable[_T]]
|
either synchronous or asynchronous. |