prefect.utilities.asyncutils
Utilities for interoperability with async functions and workers from various contexts.
GatherIncomplete
Bases: RuntimeError
Used to indicate retrieving gather results before completion
Source code in src/prefect/utilities/asyncutils.py
488 489 |
|
GatherTaskGroup
Bases: TaskGroup
A task group that gathers results.
AnyIO does not include support gather
. This class extends the TaskGroup
interface to allow simple gathering.
See https://github.com/agronholm/anyio/issues/100
This class should be instantiated with create_gather_task_group
.
Source code in src/prefect/utilities/asyncutils.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
|
start(fn, *args)
async
Since start
returns the result of task_status.started()
but here we must
return the key instead, we just won't support this method for now.
Source code in src/prefect/utilities/asyncutils.py
519 520 521 522 523 524 |
|
add_event_loop_shutdown_callback(coroutine_fn)
async
Adds a callback to the given callable on event loop closure. The callable must be a coroutine function. It will be awaited when the current event loop is shutting down.
Requires use of asyncio.run()
which waits for async generator shutdown by
default or explicit call of asyncio.shutdown_asyncgens()
. If the application
is entered with asyncio.run_until_complete()
and the user calls
asyncio.close()
without the generator shutdown call, this will not trigger
callbacks.
asyncio does not provided any other way to clean up a resource when the event loop is about to close.
Source code in src/prefect/utilities/asyncutils.py
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 481 482 483 484 485 |
|
create_gather_task_group()
Create a new task group that gathers results
Source code in src/prefect/utilities/asyncutils.py
547 548 549 550 551 552 |
|
create_task(coroutine)
Replacement for asyncio.create_task that will ensure that tasks aren't garbage collected before they complete. Allows for "fire and forget" behavior in which tasks can be created and the application can move on. Tasks can also be awaited normally.
See https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task for details (and essentially this implementation)
Source code in src/prefect/utilities/asyncutils.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
gather(*calls)
async
Run calls concurrently and gather their results.
Unlike asyncio.gather
this expects to receive callables not coroutines.
This matches anyio
semantics.
Source code in src/prefect/utilities/asyncutils.py
555 556 557 558 559 560 561 562 563 564 565 566 |
|
is_async_fn(func)
Returns True
if a function returns a coroutine.
See https://github.com/microsoft/pyright/issues/2142 for an example use
Source code in src/prefect/utilities/asyncutils.py
75 76 77 78 79 80 81 82 83 84 85 86 |
|
is_async_gen_fn(func)
Returns True
if a function is an async generator.
Source code in src/prefect/utilities/asyncutils.py
89 90 91 92 93 94 95 96 |
|
run_async_from_worker_thread(__fn, *args, **kwargs)
Runs an async function in the main thread's event loop, blocking the worker thread until completion
Source code in src/prefect/utilities/asyncutils.py
282 283 284 285 286 287 288 289 290 |
|
run_coro_as_sync(coroutine, force_new_thread=False, wait_for_result=True)
Runs a coroutine from a synchronous context, as if it were a synchronous function.
The coroutine is scheduled to run in the "run sync" event loop, which is running in its own thread and is started the first time it is needed. This allows us to share objects like async httpx clients among all coroutines running in the loop.
If run_sync is called from within the run_sync loop, it will run the coroutine in a new thread, because otherwise a deadlock would occur. Note that this behavior should not appear anywhere in the Prefect codebase or in user code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coroutine
|
Awaitable
|
The coroutine to be run as a synchronous function. |
required |
force_new_thread
|
bool
|
If True, the coroutine will always be run in a new thread. Defaults to False. |
False
|
wait_for_result
|
bool
|
If True, the function will wait for the coroutine to complete and return the result. If False, the function will submit the coroutine to the "run sync" event loop and return immediately, where it will eventually be run. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
Union[R, None]
|
The result of the coroutine if wait_for_result is True, otherwise None. |
Source code in src/prefect/utilities/asyncutils.py
182 183 184 185 186 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 |
|
run_sync_in_worker_thread(__fn, *args, **kwargs)
async
Runs a sync function in a new worker thread so that the main thread's event loop is not blocked.
Unlike the anyio function, this defaults to a cancellable thread and does not allow passing arguments to the anyio function so users can pass kwargs to their function.
Note that cancellation of threads will not result in interrupted computation, the thread may continue running — the outcome will just be ignored.
Source code in src/prefect/utilities/asyncutils.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
sync(__async_fn, *args, **kwargs)
Call an async function from a synchronous context. Block until completion.
If in an asynchronous context, we will run the code in a separate loop instead of failing but a warning will be displayed since this is not recommended.
Source code in src/prefect/utilities/asyncutils.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
|
sync_compatible(async_fn)
sync_compatible(async_fn: Callable[..., Coroutine[Any, Any, R]]) -> Callable[..., R]
sync_compatible(async_fn: Callable[..., Coroutine[Any, Any, R]]) -> Callable[..., Coroutine[Any, Any, R]]
Converts an async function into a dual async and sync function.
When the returned function is called, we will attempt to determine the best way to enter the async function.
- If in a thread with a running event loop, we will return the coroutine for the caller to await. This is normal async behavior.
- If in a blocking worker thread with access to an event loop in another thread, we will submit the async method to the event loop.
- If we cannot find an event loop, we will create a new one and run the async method then tear down the loop.
Note: Type checkers will infer functions decorated with @sync_compatible
are synchronous. If
you want to use the decorated function in an async context, you will need to ignore the types
and "cast" the return type to a coroutine. For example:
python result: Coroutine = sync_compatible(my_async_function)(arg1, arg2) # type: ignore
Source code in src/prefect/utilities/asyncutils.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 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 |
|