pending
Classes:
-
PendingCancelled–Used to indicate the pending is cancelled.
-
InvalidStateError–An invalid state of the pending.
-
Pending–A thread-safe, awaitable object representing a pending asynchronous result.
-
PendingTracker–The base class for tracking Pending.
-
PendingManager–PendingManager is a context-aware manager for tracking Pending.
-
PendingGroup–An asynchronous context manager that automatically registers pending created in its context.
PendingCancelled
¶
InvalidStateError
¶
Bases: RuntimeError
An invalid state of the pending.
Source code in src/async_kernel/pending.py
32 33 | |
Pending
¶
Bases: Awaitable[T]
A thread-safe, awaitable object representing a pending asynchronous result.
The Pending class provides a mechanism for waiting on a result or exception to be set,
either asynchronously or synchronously. It supports cancellation, metadata storage, and
callback registration for completion events.
Attributes:
-
**metadata–Arbitrary keyword arguments to associate as metadata with the instance.
Properties
metadata (dict[str, Any]): Metadata passed during creation.
Methods:
-
__init__–Initializes a new Pending object with optional creation options and metadata.
-
wait–Wait for a result or exception to be set (thread-safe) returning the pending if specified.
-
wait_sync–Wait synchronously for the a result or exception to be set (thread-safe) blocking the current thread.
-
set_result–Set the result (low-level-thread-safe).
-
set_exception–Set the exception (low-level-thread-safe).
-
cancel–Cancel the instance.
-
cancelled–Return True if the pending is cancelled.
-
set_canceller–Set a callback to handle cancellation (low-level).
-
done–Returns True if a result or exception has been set.
-
add_done_callback–Add a callback for when the pending is done (not thread-safe).
-
remove_done_callback–Remove all instances of a callback from the callbacks list.
-
result–Return the result.
-
exception–Return the exception.
Source code in src/async_kernel/pending.py
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 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 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 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 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 308 309 310 311 312 313 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 | |
REPR_OMIT
class-attribute
¶
Keys of metadata to omit when creating a repr of the instance.
metadata
property
¶
The metadata passed as keyword arguments to the instance during creation.
__init__
¶
__init__(options: PendingCreateOptions | None = None, /, **metadata: Any) -> None
Parameters:
-
(options¶PendingCreateOptions | None, default:None) –Options for creating the Pending object. If None, defaults are used.
-
(**metadata¶Any, default:{}) –Arbitrary keyword arguments containing metadata to associate with this Pending instance.
Behavior
- Initializes internal state for tracking completion and cancellation.
- Stores provided metadata in a class-level mapping.
- If options is None or allow_tracking is True in options, registers this instance with all active PendingTrackers.
-
Reference
pending
PendingTrackeradd_to_pending_trackers
Source code in src/async_kernel/pending.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
wait
async
¶
Wait for a result or exception to be set (thread-safe) returning the pending if specified.
Parameters:
-
(timeout¶float | None, default:None) –Timeout in seconds.
-
(protect¶bool, default:False) –Protect the instance from external cancellation.
-
(result¶bool, default:True) –Whether the result should be returned (use
result=Falseto avoid exceptions raised by Pending.result).
Raises:
-
TimeoutError–When the timeout expires and a result or exception has not been set.
-
PendingCancelled–If
result=Trueand the pending has been cancelled. -
Exception–If
result=Trueand an exception was set on the pending.
Source code in src/async_kernel/pending.py
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 | |
wait_sync
¶
Wait synchronously for the a result or exception to be set (thread-safe) blocking the current thread.
Parameters:
-
(timeout¶float | None, default:None) –Timeout in seconds.
-
(result¶bool, default:True) –Whether the result should be returned (use
result=Falseto avoid exceptions raised by Pending.result).
Raises:
-
TimeoutError–When the timeout expires and a result or exception has not been set.
-
PendingCancelled–If
result=Trueand the pending has been cancelled. -
Exception–If
result=Trueand an exception was set on the pending.
Warning
Blocking the thread in which the result or exception is set will cause in deadlock.
Source code in src/async_kernel/pending.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
set_result
¶
set_result(value: T, *, reset: bool = False) -> None
Set the result (low-level-thread-safe).
Parameters:
Warning
- When using reset ensure to proivide sufficient time for any waiters to retrieve the result.
Source code in src/async_kernel/pending.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
set_exception
¶
set_exception(exception: BaseException) -> None
Set the exception (low-level-thread-safe).
Source code in src/async_kernel/pending.py
233 234 235 236 237 | |
cancel
¶
Cancel the instance.
Parameters:
Notes
- Cancellation cannot be undone.
- The result will not be done until either Pending.set_result or Pending.set_exception is called.
Returns: If it has been cancelled.
Source code in src/async_kernel/pending.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
cancelled
¶
cancelled() -> bool
Return True if the pending is cancelled.
Source code in src/async_kernel/pending.py
261 262 263 | |
set_canceller
¶
Set a callback to handle cancellation (low-level).
Parameters:
-
(canceller¶Callable[[str | None], Any]) –A callback that performs the cancellation of the pending. - It must accept the cancellation message as the first argument. - The cancellation call is not thread-safe.
Notes
set_resultmust be called to mark the pending as completed.
Example
pen = Pending()
pen.cancel()
assert not pen.done()
pen.set_canceller(lambda msg: pen.set_result(None))
assert pen.done()
Source code in src/async_kernel/pending.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
done
¶
done() -> bool
Returns True if a result or exception has been set.
Source code in src/async_kernel/pending.py
292 293 294 295 296 | |
add_done_callback
¶
Add a callback for when the pending is done (not thread-safe).
If the pending is already done it will called immediately.
Source code in src/async_kernel/pending.py
298 299 300 301 302 303 304 305 306 307 | |
remove_done_callback
¶
Remove all instances of a callback from the callbacks list.
Returns the number of callbacks removed.
Source code in src/async_kernel/pending.py
309 310 311 312 313 314 315 316 317 318 319 | |
result
¶
result() -> T
Return the result.
Raises:
-
PendingCancelled–If the pending has been cancelled.
-
InvalidStateError–If the pending isn't done yet.
Source code in src/async_kernel/pending.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
exception
¶
exception() -> BaseException | None
Return the exception.
Raises:
-
PendingCancelled–If the instance has been cancelled.
Source code in src/async_kernel/pending.py
336 337 338 339 340 341 342 343 344 345 | |
PendingTracker
¶
The base class for tracking Pending.
-
Reference
typing
PendingCreateOptions
- Reference
Methods:
-
add_to_pending_trackers–Add to all active pending trackers (including subclasses of PendingTracker) in the current context.
-
current–The current instance of this class for the current context.
-
track–Track
Pendingif it isn't done. -
remove–Remove a
Pendingthat is being tracked. -
discard–Remove a
Pendingif it is being tracked.
Attributes:
-
context_id(Fixed[Self, str]) –The context id (per instance).
Source code in src/async_kernel/pending.py
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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
context_id
class-attribute
instance-attribute
¶
The context id (per instance).
add_to_pending_trackers
classmethod
¶
add_to_pending_trackers(pen: Pending) -> None
Add to all active pending trackers (including subclasses of PendingTracker) in the current context.
This method gets called automatically by Pending for all new instances (except for those that opt-out).
Source code in src/async_kernel/pending.py
383 384 385 386 387 388 389 390 391 392 393 394 | |
current
classmethod
¶
current() -> Self | None
The current instance of this class for the current context.
Source code in src/async_kernel/pending.py
396 397 398 399 400 401 | |
track
¶
track(pen: Pending) -> None
Track Pending if it isn't done.
Source code in src/async_kernel/pending.py
403 404 405 406 407 408 409 410 411 | |
remove
¶
remove(pen: Pending) -> None
Remove a Pending that is being tracked.
Source code in src/async_kernel/pending.py
413 414 415 416 417 418 419 | |
PendingManager
¶
Bases: PendingTracker
PendingManager is a context-aware manager for tracking Pending.
This class maintains a registry of Pending created within a given context, allowing for activation, deactivation, and context management using Python's contextvars. It supports manual addition and removal of Pending, and can automatically cancel outstanding tasks when deactivated.
Methods:
-
activate–Enter the active state to begin tracking pending.
-
deactivate–Leave the active state and remove pending.
-
start_tracking–Start tracking
Pendingin the current context. -
stop_tracking–Stop tracking using the token returned using start tracking.
Source code in src/async_kernel/pending.py
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 481 482 483 484 485 | |
activate
¶
activate() -> Self
Enter the active state to begin tracking pending.
Source code in src/async_kernel/pending.py
436 437 438 439 440 441 442 443 444 445 446 | |
deactivate
¶
deactivate(*, cancel_pending: bool = True) -> CountdownEvent | None
Leave the active state and remove pending.
Parameters:
Returns:
-
CountdownEvent(CountdownEvent | None) –If there are pending that have not finished cancellation.
Source code in src/async_kernel/pending.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
start_tracking
¶
Start tracking Pending in the current context.
Source code in src/async_kernel/pending.py
473 474 475 476 477 478 479 | |
stop_tracking
¶
Stop tracking using the token returned using start tracking.
Source code in src/async_kernel/pending.py
481 482 483 484 485 | |
PendingGroup
¶
Bases: PendingTracker, AsyncContextManagerMixin
An asynchronous context manager that automatically registers pending created in its context.
Any pending created within the context (except those that explicitly opt-out) are automatically added to the group.
If any pending fails, is cancelled (with the result/exception set) or the pending group is cancelled; the context will exit, and all pending will be cancelled.
Features
- The context will exit after all tracked pending are done or removed.
- Cancelled or failed pending will cancel all other pending in the group.
- Pending can be manually removed from the group while the group is active.
Parameters:
Usage
Enter the async context and create new pending.
async with PendingGroup() as pg:
assert pg.caller.to_thread(lambda: None) in pg.pending
-
Reference
caller
Callercreate_pending_group
Methods:
-
remove–Remove pen from the group.
-
cancel–Cancel the pending group (thread-safe).
-
cancelled–Return True if the pending group is cancelled.
Source code in src/async_kernel/pending.py
488 489 490 491 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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | |
remove
¶
remove(pen: Pending) -> None
Remove pen from the group.
Source code in src/async_kernel/pending.py
549 550 551 552 553 554 555 556 557 | |
cancel
¶
cancel(msg: str | None = None) -> None
Cancel the pending group (thread-safe).
Source code in src/async_kernel/pending.py
559 560 561 562 563 | |