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–Tracks the creation of
Pendingin multiple contexts. -
PendingGroup–An asynchronous context manager for tracking
Pendingthat are created in it's context. -
Pending–Pending is an internally synchronised, cancellable, awaitable class influenced by asyncio.Future.
PendingCancelled
¶
Bases: ClosedResourceError
Used to indicate the pending is cancelled.
Source code in src/async_kernel/pending.py
33 34 | |
InvalidStateError
¶
Bases: RuntimeError
An invalid state of the pending.
Source code in src/async_kernel/pending.py
37 38 | |
PendingTracker
¶
The base class for tracking Pending.
- Reference
Methods:
-
current–The instance of the active tracker in the current context.
-
active_id–The id of the active tracker in the current context.
-
add–Track
Pendinguntil it is done.
Attributes:
-
id(Fixed[Self, str]) –The unique id of the pending tracker instance.
-
pending(set[Pending[Any]]) –The pending currently associated with this instance.
Source code in src/async_kernel/pending.py
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 | |
id
class-attribute
instance-attribute
¶
The unique id of the pending tracker instance.
current
classmethod
¶
current() -> Self | None
The instance of the active tracker in the current context.
Source code in src/async_kernel/pending.py
66 67 68 69 70 71 | |
PendingManager
¶
Bases: PendingTracker
Tracks the creation of Pending in multiple contexts.
This class must be subclassed to be useful.
For each subclass there is zero or one active trackers at a time. Activating a manager will 'replace' a previously active pending manager.
Notes:
- It must be subclassed.
- Each subclass is assigned one context variable.
- This means that only one instance of the subclass can be active in a specific context at any time.
- It is linearly proportionally expensive to subclass PendingManager so it's usage should
be limited to cases where it is necessary where the exact functionality is required.
Usage:
```python
class MyPendingManager(PendingManager):
"Manages the context of ..."
m = MyPendingManager()
m2 = MyPendingManager()
# In one or more contexts
token = m.activate()
try:
...
try:
token2 = m2.activate()
pen = m2.caller.call_soon(lambda: 1 + 1)
assert pen in m2.pending
assert (
pen not in m.pending
), "pen is associated should only be associated with m2"
...
finally:
m2.deactivate(token2)
finally:
m.deactivate(token)
```
Methods:
-
activate–Start tracking
Pendingin the current context. -
deactivate–Stop tracking.
-
remove–Remove a pending from the manager.
-
context–A context manager where the pending manager is activated.
Source code in src/async_kernel/pending.py
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 | |
activate
¶
Start tracking Pending in the current context.
-
Reference
pending
PendingManagerdeactivate
Source code in src/async_kernel/pending.py
150 151 152 153 154 | |
deactivate
¶
Stop tracking.
Parameters:
Source code in src/async_kernel/pending.py
156 157 158 159 160 161 162 163 | |
remove
¶
remove(pen: Pending) -> None
Remove a pending from the manager.
Source code in src/async_kernel/pending.py
165 166 167 168 169 | |
context
¶
A context manager where the pending manager is activated.
Source code in src/async_kernel/pending.py
171 172 173 174 175 176 177 178 | |
PendingGroup
¶
Bases: PendingTracker, AsyncContextManagerMixin
An asynchronous context manager for tracking Pending that are created in it's context.
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:
-
__init__–An async context to capture all pending (that opt in) created in the context.
-
cancel–Cancel the pending group (internally synchronised).
-
cancelled–Returns: If the pending group is marked as cancelled.
Attributes:
-
cancellation_timeout–The maximum time to wait for cancelled pending to be done.
-
caller(Fixed[Self, Caller]) –The caller where the pending group was instantiated.
Source code in src/async_kernel/pending.py
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 | |
cancellation_timeout
class-attribute
instance-attribute
¶
cancellation_timeout = 10
The maximum time to wait for cancelled pending to be done.
caller
class-attribute
instance-attribute
¶
The caller where the pending group was instantiated.
__init__
¶
The pending group will only exit once all pending in the group are done.
Pending can be added to and removed from the group manually.
Parameters:
-
(shield¶bool, default:False) –Passed to the cancel scope.
-
(mode¶int, default:0) –The mode. - 0: Ignore cancellation of pending. - 1: Cancel if any pending is cancelled - raise PendingCancelled on exit. - 2: Cancel if any pending is cancelled - exit quietly.
Source code in src/async_kernel/pending.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
cancel
¶
Cancel the pending group (internally synchronised).
Source code in src/async_kernel/pending.py
295 296 297 298 299 300 301 302 | |
Pending
¶
Bases: Awaitable[T]
Pending is an internally synchronised, cancellable, awaitable class influenced by asyncio.Future.
- It can be wait/awaited and cancelled from any thread (not considering deadlocks)
- Provides metadata storage
- Has built in support for tracking pending created in a specific context (see: PendingManager and PendingGroup).
Properties
- Pending.metadata: Metadata associated with the pending.
- Pending.context: The context associated with the pending.
High level methods
- Pending.wait: Wait asynchronously for the pending to be complete.
- Pending.wait_sync: Wait synchronously for the pending to be complete.
- Pending.cancel: Cancel the pending (result must be set to finalize cancellation).
- Pending.result: Get the result of the pending.
- Pending.exception: Get the exception of the pending.
Low level methods
- Pending.add_done_callback: Add a callback that is called once the pending is complete.
- Pending.remove_done_callback: Remove a previously added callback.
- Pending.set_canceller: Set a callback to handle cancellation.
Methods:
-
__init__–Initializes a new pending object with optional creation options and metadata.
-
wait–Wait for
resultorexceptionto be set (internally synchronised) returning the result if specified. -
wait_sync–Wait synchronously for
resultorexception(internally synchronised) returning the result if specified. -
set_result–Set the result (low-level internally synchronised).
-
set_exception–Set the exception (low-level internally synchronised).
-
cancel–Cancel the pending if it is not already
done. -
cancel_wait–Cancel the pending and wait for it to be
done. -
cancelled–Returns:
-
set_canceller–Set a callback to handle cancellation (low-level).
-
done–Returns
Trueif a result or exception has been set. -
add_done_callback–Add a callback for when the pending is
done. -
remove_done_callback–Remove
fnfrom the done callback list. -
result–Return the result.
-
exception–Return the exception.
Attributes:
-
metadata(dict[str, Any]) –The metadata associated with the pending.
-
context(Context | None) –The context associated with Pending.
Source code in src/async_kernel/pending.py
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 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
metadata
property
¶
__init__
¶
__init__(
context: Context | None = None,
trackers: type[PendingTracker] | tuple[type[PendingTracker], ...] = (),
/,
**metadata: Any,
) -> None
Parameters:
-
(context¶Context | None, default:None) –A context to associate with the pending, if provided it is copied.
-
(trackers¶type[PendingTracker] | tuple[type[PendingTracker], ...], default:()) –A subclass or tuple of
PendingTrackersubclasses to which the pending can be added in the current context. -
(**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.
Source code in src/async_kernel/pending.py
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 | |
wait
async
¶
Wait for result or exception to be set (internally synchronised) returning the result if specified.
Parameters:
-
(timeout¶float | None, default:None) –Timeout in seconds.
-
(protect¶bool, default:False) –Protect the pending from a
TimeoutErroror external cancellation. -
(result¶bool, default:True) –If
resultshould be returned.
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.
Tip
To wait for a cancelled pending to complete use await pen.wait(result=False).
Source code in src/async_kernel/pending.py
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 | |
wait_sync
¶
wait_sync(
*, timeout: float | None = None, protect: bool = False, result: bool = True
) -> T | None
Wait synchronously for result or exception (internally synchronised) returning the result if specified.
Parameters:
-
(timeout¶float | None, default:None) –Timeout in seconds.
-
(protect¶bool, default:False) –Protect the pending from cancellation from a
TimeoutError. -
(result¶bool, default:True) –When
Truethe result is returned.
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
Calling this method in the same thread where the result or exception is set will result in deadlock unless a greenlet based event library is in use.
Source code in src/async_kernel/pending.py
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 | |
set_result
¶
set_result(value: T) -> None
Set the result (low-level internally synchronised).
Parameters:
-
(value¶T) –The result to set.
Raises:
-
InvalidStateError–If the pending is already done.
-
Reference
pending
Pendingset_canceller
Source code in src/async_kernel/pending.py
537 538 539 540 541 542 543 544 545 546 547 | |
set_exception
¶
set_exception(exception: BaseException) -> None
Set the exception (low-level internally synchronised).
Parameters:
-
(exception¶BaseException) –The value to set as the exception.
Raises:
-
InvalidStateError–If the pending is already done.
-
Reference
pending
Pendingset_canceller
Source code in src/async_kernel/pending.py
549 550 551 552 553 554 555 556 557 558 559 | |
cancel
¶
Cancel the pending if it is not already done.
Parameters:
Returns:
-
bool–If the pending is marked as cancelled.
Notes
- Cancellation cannot be undone.
- Multiple calls append
msgto the cancellation message with no other effect. - The
donestate is a function of the canceller, if no canceller is set,donewill be set immediately.
Source code in src/async_kernel/pending.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |
cancel_wait
async
¶
Cancel the pending and wait for it to be done.
Source code in src/async_kernel/pending.py
590 591 592 593 594 595 | |
cancelled
¶
cancelled() -> bool
Returns:
-
bool–If the pending is marked as cancelled.
Notes:
- This can return `True` before a pending is `done`.
- To wait for a pending to complete (assuming a canceller is or will be set); use
`await pen.wait(result=False)` or `pen.wait_sync(result=False)`
Source code in src/async_kernel/pending.py
597 598 599 600 601 602 603 604 605 606 607 608 | |
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.
cancellermust be externally synchronised.
Warning
- set_result or set_exception MUST be called by the
canceller, or as a result of the call tocancellerto mark the pending asdone.
Raises:
-
InvalidStateError–If already
done. -
PendingCancelled–If already
cancelled.
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
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
done
¶
done() -> bool
Returns True if a result or exception has been set.
Source code in src/async_kernel/pending.py
643 644 645 646 647 | |
add_done_callback
¶
Add a callback for when the pending is done.
The callback should be light weight and provide its own thread safety.
Callbacks added are handled FIFO.
Source code in src/async_kernel/pending.py
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
remove_done_callback
¶
Remove fn from the done callback list.
Returns the number of items removed.
Source code in src/async_kernel/pending.py
665 666 667 668 669 670 671 672 673 674 675 | |
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
677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
exception
¶
exception() -> BaseException | None
Return the exception.
Raises:
-
PendingCancelled–If the pending has been cancelled.
Source code in src/async_kernel/pending.py
692 693 694 695 696 697 698 699 700 701 | |