prefect.futures
PrefectConcurrentFuture
Bases: PrefectWrappedFuture[R, Future]
A Prefect future that wraps a concurrent.futures.Future. This future is used when the task run is submitted to a ThreadPoolExecutor.
Source code in src/prefect/futures.py
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
PrefectDistributedFuture
Bases: PrefectFuture[R]
Represents the result of a computation happening anywhere.
This class is typically used to interact with the result of a task run scheduled to run in a Prefect task worker but can be used to interact with any task run scheduled in Prefect's API.
Source code in src/prefect/futures.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
PrefectFuture
Bases: ABC
, Generic[R]
Abstract base class for Prefect futures. A Prefect future is a handle to the asynchronous execution of a task run. It provides methods to wait for the task to complete and to retrieve the result of the task run.
Source code in src/prefect/futures.py
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 |
|
state: State
property
The current state of the task run associated with this future
task_run_id: uuid.UUID
property
The ID of the task run associated with this future
add_done_callback(fn)
abstractmethod
Add a callback to be run when the future completes or is cancelled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn
|
A callable that will be called with this future as its only argument when the future completes or is cancelled. |
required |
Source code in src/prefect/futures.py
94 95 96 97 98 99 100 101 102 |
|
PrefectFutureList
Bases: list
, Iterator
, Generic[F]
A list of Prefect futures.
This class provides methods to wait for all futures in the list to complete and to retrieve the results of all task runs.
Source code in src/prefect/futures.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
result(timeout=None, raise_on_failure=True)
Get the results of all task runs associated with the futures in the list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
Optional[float]
|
The maximum number of seconds to wait for all futures to complete. |
None
|
raise_on_failure
|
bool
|
If |
True
|
Returns:
Type | Description |
---|---|
List[R]
|
A list of results of the task runs. |
Raises:
Type | Description |
---|---|
TimeoutError
|
If the timeout is reached before all futures complete. |
Source code in src/prefect/futures.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
wait(timeout=None)
Wait for all futures in the list to complete.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
Optional[float]
|
The maximum number of seconds to wait for all futures to complete. This method will not raise if the timeout is reached. |
None
|
Source code in src/prefect/futures.py
290 291 292 293 294 295 296 297 298 |
|
PrefectWrappedFuture
Bases: PrefectFuture
, ABC
, Generic[R, F]
A Prefect future that wraps another future object.
Source code in src/prefect/futures.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
wrapped_future: F
property
The underlying future object wrapped by this Prefect future
resolve_futures_to_states(expr)
Given a Python built-in collection, recursively find PrefectFutures
and build a
new collection with the same structure with futures resolved to their final states.
Resolving futures to their final states may wait for execution to complete.
Unsupported object types will be returned without modification.
Source code in src/prefect/futures.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
wait(futures, timeout=None)
Wait for the futures in the given sequence to complete.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
futures
|
List[PrefectFuture[R]]
|
The sequence of Futures to wait upon. |
required |
timeout
|
The maximum number of seconds to wait. If None, then there is no limit on the wait time. |
None
|
Returns:
Type | Description |
---|---|
DoneAndNotDoneFutures
|
A named 2-tuple of sets. The first set, named 'done', contains the |
DoneAndNotDoneFutures
|
futures that completed (is finished or cancelled) before the wait |
DoneAndNotDoneFutures
|
completed. The second set, named 'not_done', contains uncompleted |
DoneAndNotDoneFutures
|
futures. Duplicate futures given to futures are removed and will be |
DoneAndNotDoneFutures
|
returned only once. |
Examples:
@task
def sleep_task(seconds):
sleep(seconds)
return 42
@flow
def flow():
futures = random_task.map(range(10))
done, not_done = wait(futures, timeout=5)
print(f"Done: {len(done)}")
print(f"Not Done: {len(not_done)}")
Source code in src/prefect/futures.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
|