pending
Classes:
-
PendingCancelled–Used to indicate the pending is cancelled.
-
InvalidStateError–An invalid state of the pending.
-
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.
-
Pending–A thread-safe, cancellable, awaitable object representing a pending asynchronous result.
PendingCancelled
¶
InvalidStateError
¶
Bases: RuntimeError
An invalid state of the pending.
Source code in src/async_kernel/pending.py
32 33 | |
PendingTracker
¶
The base class for tracking Pending.
- 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.
-
start_tracking–Start tracking
Pendingin the current context. -
stop_tracking–Stop tracking using the token.
-
add–Track
Pendinguntil it is done. -
on_pending_done–A done_callback that is registered with pen when it is added (don't call directly).
-
remove–Remove a
Pending. -
discard–Discard the
Pending.
Attributes:
-
context_id(Fixed[Self, str]) –The context id (per instance).
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 | |
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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
current
classmethod
¶
current() -> Self | None
The current instance of this class for the current context.
Source code in src/async_kernel/pending.py
84 85 86 87 88 89 | |
start_tracking
¶
Start tracking Pending in the current context.
-
Reference
pending
PendingTrackerstop_tracking
Source code in src/async_kernel/pending.py
91 92 93 94 95 96 97 98 99 100 101 102 | |
stop_tracking
¶
Stop tracking using the token.
Parameters:
-
(token¶Token[str | None]) –The token returned from start_tracking.
Source code in src/async_kernel/pending.py
104 105 106 107 108 109 110 111 112 113 | |
add
¶
add(pen: Pending) -> None
Track Pending until it is done.
Source code in src/async_kernel/pending.py
115 116 117 118 119 120 121 122 | |
on_pending_done
¶
on_pending_done(pen: Pending) -> None
A done_callback that is registered with pen when it is added (don't call directly).
Source code in src/async_kernel/pending.py
124 125 126 | |
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 cancelling all 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 | |
activate
¶
activate() -> Self
Enter the active state to begin tracking pending.
Source code in src/async_kernel/pending.py
150 151 152 153 154 155 156 157 158 | |
deactivate
¶
deactivate() -> None
Leave the active state cancelling all pending.
Source code in src/async_kernel/pending.py
160 161 162 163 164 165 166 167 | |
PendingGroup
¶
Bases: PendingTracker, AsyncContextManagerMixin
An asynchronous context manager that automatically registers pending created in its context.
All pending created within the context of PendingGroup provided that the PendingGroup is an instance
of Pending.trackers will be automatically added to the group (default for Pending).
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
Methods:
-
cancel–Cancel the pending group (thread-safe).
-
cancelled–Return True if the pending group is cancelled.
Attributes:
-
cancellation_timeout–The maximum time to wait for cancelled pending to be done.
Source code in src/async_kernel/pending.py
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 | |
cancellation_timeout
class-attribute
instance-attribute
¶
cancellation_timeout = 10
The maximum time to wait for cancelled pending to be done.
cancel
¶
Cancel the pending group (thread-safe).
Source code in src/async_kernel/pending.py
259 260 261 262 263 264 265 266 267 268 | |
Pending
¶
Bases: Awaitable[T]
A thread-safe, cancellable, 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.
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.
Attributes:
-
REPR_OMIT(set[str]) –Keys of metadata to omit when creating a repr of the instance.
-
metadata(dict[str, Any]) –The metadata passed as keyword arguments to the instance during creation.
-
trackers(type[PendingTracker] | tuple[type[PendingTracker], ...]) –A tuple of async_kernel.pending.PendingTracker subclasses that the pending is permitted to register with.
Source code in src/async_kernel/pending.py
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 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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 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 481 482 483 484 485 486 487 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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | |
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.
trackers
instance-attribute
¶
trackers: type[PendingTracker] | tuple[type[PendingTracker], ...] = trackers
A tuple of async_kernel.pending.PendingTracker subclasses that the pending is permitted to register with.
Should be specified during init.
For some pending it may not make sense for it to be added to a PendingGroup
Instead specify (PendingManager,) instead of (PendingTracker,).
- Reference
__init__
¶
__init__(
trackers: type[PendingTracker] | tuple[type[PendingTracker], ...] = (),
**metadata: Any,
)
Parameters:
-
(trackers¶type[PendingTracker] | tuple[type[PendingTracker], ...], default:()) –A subclass or tuple of
PendingTrackersubclasses to which the pending can be added given the context. -
(**metadata¶Any, default:{}) –Arbitrary keyword arguments containing metadata to associate with this Pending instance. trackers: Enabled by default. To disable tracking pass
trackers=False
Behavior
- Initializes internal state for tracking completion and cancellation
- Stores provided metadata in a class-level mapping
- Registers with async_kernel.pending.PendingTracker.add_to_pending_trackers
-
Reference
pending
PendingTrackeradd_to_pending_trackers
Source code in src/async_kernel/pending.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
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
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 | |
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
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
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
457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
set_exception
¶
set_exception(exception: BaseException) -> None
Set the exception (low-level-thread-safe).
Source code in src/async_kernel/pending.py
472 473 474 475 476 | |
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
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
cancelled
¶
cancelled() -> bool
Return True if the pending is cancelled.
Source code in src/async_kernel/pending.py
500 501 502 | |
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
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 | |
done
¶
done() -> bool
Returns True if a result or exception has been set.
Source code in src/async_kernel/pending.py
531 532 533 534 535 | |
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
537 538 539 540 541 542 543 544 545 546 | |
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
548 549 550 551 552 553 554 555 556 557 558 | |
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
560 561 562 563 564 565 566 567 568 569 570 571 572 573 | |
exception
¶
exception() -> BaseException | None
Return the exception.
Raises:
-
PendingCancelled–If the instance has been cancelled.
Source code in src/async_kernel/pending.py
575 576 577 578 579 580 581 582 583 584 | |