Future
Asynchronous computation using futures.
future_value = Future.from_value
module-attribute
An alias of Future.from_value
.
Future
Bases: Awaitable[T]
Represents future computations.
Source code in src/wraps/futures/future.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 152 153 154 155 |
|
create(awaitable: Awaitable[U]) -> Future[U]
classmethod
Creates a Future[U]
from an Awaitable[U]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
awaitable |
Awaitable[U]
|
The awaitable to wrap. |
required |
Returns:
Type | Description |
---|---|
Future[U]
|
The future wrapping the given awaitable. |
Source code in src/wraps/futures/future.py
39 40 41 42 43 44 45 46 47 48 49 50 |
|
future_map(function: Unary[T, U]) -> Future[U]
Maps a Future[T]
to a Future[U]
by applying the function
to its result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
Unary[T, U]
|
The function to apply. |
required |
Returns:
Type | Description |
---|---|
Future[U]
|
The mapped future. |
Source code in src/wraps/futures/future.py
52 53 54 55 56 57 58 59 60 61 62 |
|
future_map_await(function: AsyncUnary[T, U]) -> Future[U]
Maps a Future[T]
to a Future[U]
by applying the asynchronous function
to its result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
AsyncUnary[T, U]
|
The asynchronous function to apply. |
required |
Returns:
Type | Description |
---|---|
Future[U]
|
The mapped future. |
Source code in src/wraps/futures/future.py
64 65 66 67 68 69 70 71 72 73 74 75 |
|
then(function: FutureUnary[T, U]) -> Future[U]
Chains computation by applying the function
to the result, returning the resulting
Future[U]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
FutureUnary[T, U]
|
The future-returning function to apply. |
required |
Returns:
Type | Description |
---|---|
Future[U]
|
The resulting future. |
Source code in src/wraps/futures/future.py
83 84 85 86 87 88 89 90 91 92 93 |
|
future_flatten() -> Future[U]
Flattens a Future[Future[U]]
to a Future[U]
.
This is identical to:
future.then(identity)
Returns:
Type | Description |
---|---|
Future[U]
|
The flattened future. |
Source code in src/wraps/futures/future.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
async_iter() -> AsyncIterator[T]
async
Creates an asynchronous iterator yielding the result of this
Future[T]
.
Returns:
Type | Description |
---|---|
AsyncIterator[T]
|
An asynchronous iterator yielding the result of the future. |
Source code in src/wraps/futures/future.py
116 117 118 119 120 121 122 123 |
|
from_value(value: U) -> Future[U]
classmethod
Wraps the value
of type U
into a Future[U]
.
This is functionally the same as:
async def async_identity(value: T) -> T:
return value
value = 42
future = Future(async_identity(value))
Example
value = 42
future = Future.from_value(value)
assert await future is value
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
U
|
The value to wrap. |
required |
Returns:
Type | Description |
---|---|
Future[U]
|
The future wrapping the given value. |
Source code in src/wraps/futures/future.py
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 152 153 154 155 |
|
wrap_future(function: AsyncCallable[P, T]) -> FutureCallable[P, T]
Wraps the asynchronous function
returning T
into the function
returning Future[T]
.
Example
@wrap_future
async def function() -> int:
return 42
string = "42"
result = await function().future_map(str)
assert result == string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function |
AsyncCallable[P, T]
|
The asynchronous function to wrap. |
required |
Returns:
Type | Description |
---|---|
FutureCallable[P, T]
|
The wrapping function. |
Source code in src/wraps/futures/future.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|