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 prefect/utilities/asyncutils.py
310 311 |
|
GatherTaskGroup
¶
Bases: anyio.abc.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 prefect/utilities/asyncutils.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 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 |
|
start
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 prefect/utilities/asyncutils.py
341 342 343 344 345 346 |
|
add_event_loop_shutdown_callback
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 prefect/utilities/asyncutils.py
274 275 276 277 278 279 280 281 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 |
|
create_gather_task_group
¶
Create a new task group that gathers results
Source code in prefect/utilities/asyncutils.py
369 370 371 372 373 374 |
|
gather
async
¶
Run calls concurrently and gather their results.
Unlike asyncio.gather
this expects to receieve callables not coroutines.
This matches anyio
semantics.
Source code in prefect/utilities/asyncutils.py
377 378 379 380 381 382 383 384 385 386 387 388 |
|
is_async_fn
¶
Returns True
if a function returns a coroutine.
See https://github.com/microsoft/pyright/issues/2142 for an example use
Source code in prefect/utilities/asyncutils.py
53 54 55 56 57 58 59 60 61 62 63 64 |
|
is_async_gen_fn
¶
Returns True
if a function is an async generator.
Source code in prefect/utilities/asyncutils.py
67 68 69 70 71 72 73 74 |
|
raise_async_exception_in_thread
¶
Raise an exception in a thread asynchronously.
This will not interrupt long-running system calls like sleep
or wait
.
Source code in prefect/utilities/asyncutils.py
96 97 98 99 100 101 102 103 104 105 106 |
|
run_async_from_worker_thread
¶
Runs an async function in the main thread's event loop, blocking the worker thread until completion
Source code in prefect/utilities/asyncutils.py
169 170 171 172 173 174 175 176 177 |
|
run_sync_in_interruptible_worker_thread
async
¶
Runs a sync function in a new interruptible worker thread so that the main thread's event loop is not blocked
Unlike the anyio function, this performs best-effort cancellation of the
thread using the C API. Cancellation will not interrupt system calls like
sleep
.
Source code in prefect/utilities/asyncutils.py
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 156 157 158 159 160 161 162 163 164 165 166 |
|
run_sync_in_worker_thread
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 prefect/utilities/asyncutils.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
sync
¶
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 prefect/utilities/asyncutils.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
sync_compatible
¶
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.
Source code in prefect/utilities/asyncutils.py
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 |
|