Skip to content

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

Bases: ClosedResourceError

Used to indicate the pending is cancelled.

Source code in src/async_kernel/pending.py
28
29
class PendingCancelled(anyio.ClosedResourceError):
    "Used to indicate the pending is cancelled."

InvalidStateError

Bases: RuntimeError

An invalid state of the pending.

Source code in src/async_kernel/pending.py
32
33
class InvalidStateError(RuntimeError):
    "An invalid state of the pending."

PendingTracker

The base class for tracking Pending.

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 Pending in the current context.

  • stop_tracking

    Stop tracking using the token.

  • add

    Track Pending until 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:

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
class PendingTracker:
    """
    The base class for tracking [Pending][async_kernel.pending.Pending].
    """

    _active_classes: ClassVar[set[type[Self]]] = set()
    _active_trackers: ClassVar[dict[str, Self]] = {}
    _contextvar: ClassVar[contextvars.ContextVar[str | None]] = contextvars.ContextVar("PendingManager", default=None)

    _active = False
    _pending: Fixed[Self, set[Pending[Any]]] = Fixed(set)
    _tracking: bool = False
    _parent_context_id: None | str = None

    context_id: Fixed[Self, str] = Fixed(lambda _: str(uuid.uuid4()))
    "The context id (per instance)."

    @property
    def active(self) -> bool:
        return self._active

    @property
    def pending(self) -> set[Pending[Any]]:
        return self._pending.copy()

    def __init_subclass__(cls) -> None:
        # Each subclass is assigned a new context variable.
        cls._contextvar = contextvars.ContextVar(f"{cls.__module__}.{cls.__name__}", default=None)
        return super().__init_subclass__()

    @classmethod
    def add_to_pending_trackers(cls, pen: Pending) -> None:
        """
        Add to all active pending trackers (including subclasses of PendingTracker) in the current context.

        This method gets called automatically by [Pending][async_kernel.pending.Pending.__init__]
        for all new instances (except for those that opt-out).
        """
        # Called by `Pending` when a new instance for each new instance.
        if trackers := pen.trackers:
            for cls_ in cls._active_classes:
                if (
                    issubclass(cls_, trackers)
                    and (id_ := cls_._contextvar.get())
                    and (pm := cls._active_trackers.get(id_))
                ):
                    pm.add(pen)

    @classmethod
    def current(cls) -> Self | None:
        "The current instance of this class for the current context."
        if (id_ := cls._contextvar.get()) and (current := cls._active_trackers.get(id_)):
            return current
        return None

    def start_tracking(self) -> contextvars.Token[str | None]:
        """
        Start tracking `Pending` in the  current context.
        """
        if self._tracking or not self.active:
            raise InvalidStateError
        assert self._active
        self._active_classes.add(self.__class__)
        self._active_trackers[self.context_id] = self
        self._parent_context_id = self._contextvar.get()
        self._tracking = True
        return self._contextvar.set(self.context_id)

    def stop_tracking(self, token: contextvars.Token[str | None]) -> None:
        """
        Stop tracking using the token.

        Args:
            token: The token returned from [start_tracking][].
        """
        self._contextvar.reset(token)
        self._tracking = False
        self._parent_context_id = None

    def add(self, pen: Pending) -> None:
        "Track `Pending` until it is done."

        if self._active and isinstance(self, pen.trackers) and (pen not in self._pending):
            self._pending.add(pen)
            pen.add_done_callback(self.on_pending_done)
        if (id_ := self._parent_context_id) and (parent := self._active_trackers.get(id_)):
            parent.add(pen)

    def on_pending_done(self, pen: Pending) -> None:
        "A done_callback that is registered with pen when it is added (don't call directly)."
        self._pending.discard(pen)

    def remove(self, pen: Pending) -> None:
        "Remove a `Pending`."
        self._pending.remove(pen)
        pen.remove_done_callback(self.discard)

    def discard(self, pen: Pending) -> None:
        "Discard the `Pending`."
        try:
            self.remove(pen)
        except IndexError:
            pass

context_id class-attribute instance-attribute

context_id: Fixed[Self, str] = Fixed(lambda _: str(uuid4()))

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
@classmethod
def add_to_pending_trackers(cls, pen: Pending) -> None:
    """
    Add to all active pending trackers (including subclasses of PendingTracker) in the current context.

    This method gets called automatically by [Pending][async_kernel.pending.Pending.__init__]
    for all new instances (except for those that opt-out).
    """
    # Called by `Pending` when a new instance for each new instance.
    if trackers := pen.trackers:
        for cls_ in cls._active_classes:
            if (
                issubclass(cls_, trackers)
                and (id_ := cls_._contextvar.get())
                and (pm := cls._active_trackers.get(id_))
            ):
                pm.add(pen)

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
@classmethod
def current(cls) -> Self | None:
    "The current instance of this class for the current context."
    if (id_ := cls._contextvar.get()) and (current := cls._active_trackers.get(id_)):
        return current
    return None

start_tracking

start_tracking() -> Token[str | None]

Start tracking Pending in the current context.

Source code in src/async_kernel/pending.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def start_tracking(self) -> contextvars.Token[str | None]:
    """
    Start tracking `Pending` in the  current context.
    """
    if self._tracking or not self.active:
        raise InvalidStateError
    assert self._active
    self._active_classes.add(self.__class__)
    self._active_trackers[self.context_id] = self
    self._parent_context_id = self._contextvar.get()
    self._tracking = True
    return self._contextvar.set(self.context_id)

stop_tracking

stop_tracking(token: Token[str | None]) -> None

Stop tracking using the token.

Parameters:

Source code in src/async_kernel/pending.py
104
105
106
107
108
109
110
111
112
113
def stop_tracking(self, token: contextvars.Token[str | None]) -> None:
    """
    Stop tracking using the token.

    Args:
        token: The token returned from [start_tracking][].
    """
    self._contextvar.reset(token)
    self._tracking = False
    self._parent_context_id = None

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
def add(self, pen: Pending) -> None:
    "Track `Pending` until it is done."

    if self._active and isinstance(self, pen.trackers) and (pen not in self._pending):
        self._pending.add(pen)
        pen.add_done_callback(self.on_pending_done)
    if (id_ := self._parent_context_id) and (parent := self._active_trackers.get(id_)):
        parent.add(pen)

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
def on_pending_done(self, pen: Pending) -> None:
    "A done_callback that is registered with pen when it is added (don't call directly)."
    self._pending.discard(pen)

remove

remove(pen: Pending) -> None

Remove a Pending.

Source code in src/async_kernel/pending.py
128
129
130
131
def remove(self, pen: Pending) -> None:
    "Remove a `Pending`."
    self._pending.remove(pen)
    pen.remove_done_callback(self.discard)

discard

discard(pen: Pending) -> None

Discard the Pending.

Source code in src/async_kernel/pending.py
133
134
135
136
137
138
def discard(self, pen: Pending) -> None:
    "Discard the `Pending`."
    try:
        self.remove(pen)
    except IndexError:
        pass

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
class PendingManager(PendingTracker):
    """
    PendingManager is a context-aware manager for tracking [Pending][async_kernel.pending.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.
    """

    def activate(self) -> Self:
        """
        Enter the active state to begin tracking pending.
        """
        assert not self._active
        self._active_trackers[self.context_id] = self
        self._active_classes.add(self.__class__)
        self._active = True
        return self

    def deactivate(self) -> None:
        """
        Leave the active state cancelling all pending.
        """
        self._active = False
        self._active_trackers.pop(self.context_id, None)
        for pen in self._pending.copy():
            pen.cancel(f"{self} has been deactivated")

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
def activate(self) -> Self:
    """
    Enter the active state to begin tracking pending.
    """
    assert not self._active
    self._active_trackers[self.context_id] = self
    self._active_classes.add(self.__class__)
    self._active = True
    return self

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
def deactivate(self) -> None:
    """
    Leave the active state cancelling all pending.
    """
    self._active = False
    self._active_trackers.pop(self.context_id, None)
    for pen in self._pending.copy():
        pen.cancel(f"{self} has been deactivated")

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:

  • shield

    (bool, default: False ) –

    Shield from external cancellation.

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:

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
class PendingGroup(PendingTracker, anyio.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.

    Args:
        shield: [Shield][anyio.CancelScope.shield] from external cancellation.

    Usage:
        Enter the async context and create new pending.

        ```python
        async with PendingGroup() as pg:
            assert pg.caller.to_thread(lambda: None) in pg.pending
        ```
    """

    _cancel_scope: anyio.CancelScope
    _cancelled: str | None = None
    cancellation_timeout = 10
    "The maximum time to wait for cancelled pending to be done."

    caller = Fixed(lambda _: async_kernel.Caller())

    def __init__(self, *, shield: bool = False) -> None:
        self.caller  # noqa: B018
        self._shield = shield
        super().__init__()

    @contextlib.asynccontextmanager
    async def __asynccontextmanager__(self) -> AsyncGenerator[Self]:
        self._cancel_scope = anyio.CancelScope(shield=self._shield)
        self._all_done = create_async_event()
        self._active = True
        self._leaving_context = False
        token = self.start_tracking()
        try:
            with self._cancel_scope:
                try:
                    yield self
                    self._leaving_context = True
                    if self._pending:
                        await self._all_done
                except (anyio.get_cancelled_exc_class(), Exception) as e:
                    self.cancel(f"An error occurred: {e!r}")
                    raise
            if self._cancelled is not None:
                raise PendingCancelled(self._cancelled)
        finally:
            self._leaving_context = True
            self.stop_tracking(token)
            if self._pending:
                if self._all_done or self._all_done.cancelled():
                    self._all_done = create_async_event()
                if self._pending and not self._all_done:
                    with anyio.CancelScope(shield=True), anyio.move_on_after(self.cancellation_timeout):
                        await self._all_done
            self._active = False

    @override
    def add(self, pen: Pending):
        assert self._active
        if self._cancelled is not None:
            msg = f"Trying to add to a cancelled PendingGroup.\nCancellation messages: {self._cancelled}"
            pen.cancel(msg)
        else:
            super().add(pen)

    @override
    def on_pending_done(self, pen: Pending) -> None:
        try:
            self._pending.remove(pen)
            if self._active and (not pen.cancelled() and (pen.exception())):
                self.cancel(f"Exception in member: {pen}")
        except KeyError:
            pass
        if self._leaving_context and not self._pending:
            self._all_done.set()

    @enable_signal_safety
    def cancel(self, msg: str | None = None) -> bool:
        "Cancel the pending group (thread-safe)."
        if self._active:
            self._cancelled = "\n".join(((self._cancelled or ""), msg or ""))
            if not self._cancel_scope.cancel_called:
                self.caller.call_direct(self._cancel_scope.cancel, msg)
                for pen_ in self.pending:
                    pen_.cancel(msg)
        return self.cancelled()

    def cancelled(self) -> bool:
        """Return True if the pending group is cancelled."""
        return bool(self._cancelled)

cancellation_timeout class-attribute instance-attribute

cancellation_timeout = 10

The maximum time to wait for cancelled pending to be done.

cancel

cancel(msg: str | None = None) -> bool

Cancel the pending group (thread-safe).

Source code in src/async_kernel/pending.py
259
260
261
262
263
264
265
266
267
268
@enable_signal_safety
def cancel(self, msg: str | None = None) -> bool:
    "Cancel the pending group (thread-safe)."
    if self._active:
        self._cancelled = "\n".join(((self._cancelled or ""), msg or ""))
        if not self._cancel_scope.cancel_called:
            self.caller.call_direct(self._cancel_scope.cancel, msg)
            for pen_ in self.pending:
                pen_.cancel(msg)
    return self.cancelled()

cancelled

cancelled() -> bool

Return True if the pending group is cancelled.

Source code in src/async_kernel/pending.py
270
271
272
def cancelled(self) -> bool:
    """Return True if the pending group is cancelled."""
    return bool(self._cancelled)

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:

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
class Pending(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.
    """

    __slots__ = [
        "__weakref__",
        "_cancelled",
        "_canceller",
        "_done",
        "_done_callbacks",
        "_exception",
        "_result",
        "trackers",
    ]

    REPR_OMIT: ClassVar[set[str]] = {"func", "args", "kwargs"}
    "Keys of metadata to omit when creating a repr of the instance."

    _metadata_mappings: ClassVar[dict[int, dict[str, Any]]] = {}
    "A mapping of instance's id its metadata."

    _cancelled: str | None
    _canceller: Callable[[str | None], Any]
    _exception: Exception
    _done: bool
    _result: T
    trackers: type[PendingTracker] | tuple[type[PendingTracker], ...]
    """
    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,)`.
    """

    @property
    def metadata(self) -> dict[str, Any]:
        """
        The metadata passed as keyword arguments to the instance during creation.
        """
        return self._metadata_mappings[id(self)]

    def __init__(self, trackers: type[PendingTracker] | tuple[type[PendingTracker], ...] = (), **metadata: Any):
        """
        Initializes a new Pending object with optional creation options and metadata.

        Args:
            trackers: A subclass or tuple of `PendingTracker` subclasses to which the pending can be added given the context.
            **metadata: 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][]
        """
        self._done_callbacks: deque[Callable[[Self], Any]] = deque()
        self._metadata_mappings[id(self)] = metadata
        self._done = False
        self._cancelled = None
        self.trackers = trackers
        if trackers:
            PendingTracker.add_to_pending_trackers(self)

    def __del__(self):
        self._metadata_mappings.pop(id(self), None)

    @override
    def __repr__(self) -> str:
        rep = (
            "<Pending"
            + ((" ⛔" + (f"message={self._cancelled!s}" if self._cancelled else "")) if self.cancelled() else "")
            + ((f" ❗ {e!r}" if (e := getattr(self, "_exception", None)) else " 🏁") if self._done else " 🏃")
        )
        rep = f"{rep} at {id(self)}"
        with contextlib.suppress(Exception):
            if md := self.metadata:
                rep = f"{rep} metadata:"
                if "func" in md:
                    items = [f"{k}={truncated_rep.repr(v)}" for k, v in md.items() if k not in self.REPR_OMIT]
                    rep += f" | {md['func']} {' | '.join(items) if items else ''}"
                else:
                    rep += f"{truncated_rep.repr(md)}"
        return rep + " >"

    @override
    def __await__(self) -> Generator[Any, None, T]:
        return self.wait().__await__()

    if TYPE_CHECKING:

        @overload
        async def wait(
            self, *, timeout: float | None = ..., protect: bool = False | ..., result: Literal[True] = True
        ) -> T: ...

        @overload
        async def wait(self, *, timeout: float | None = ..., protect: bool = ..., result: Literal[False]) -> None: ...

    async def wait(self, *, timeout: float | None = None, protect: bool = False, result: bool = True) -> T | None:
        """
        Wait for a result or exception to be set (thread-safe) returning the pending if specified.

        Args:
            timeout: Timeout in seconds.
            protect: Protect the instance from external cancellation.
            result: Whether the result should be returned (use `result=False` to avoid exceptions raised by [Pending.result][]).

        Raises:
            TimeoutError: When the timeout expires and a result or exception has not been set.
            PendingCancelled: If `result=True` and the pending has been cancelled.
            Exception: If `result=True` and an exception was set on the pending.
        """
        try:
            if not self._done or self._done_callbacks:
                event = create_async_event()
                self._done_callbacks.appendleft(lambda _: event.set())
                with anyio.fail_after(timeout):
                    if not self._done or self._done_callbacks:
                        await event
            else:
                await async_checkpoint(force=True)
            return self.result() if result else None
        except (anyio.get_cancelled_exc_class(), TimeoutError) as e:
            if not protect:
                self.cancel(f"Cancelled due to cancellation or timeout: {e}.")
            raise

    if TYPE_CHECKING:

        @overload
        def wait_sync(self, *, timeout: float | None = ..., result: Literal[True] = True) -> T: ...

        @overload
        def wait_sync(self, *, timeout: float | None = ..., result: Literal[False]) -> None: ...

    def wait_sync(self, *, timeout: float | None = None, result: bool = True) -> T | None:
        """
        Wait synchronously for the a result or exception to be set (thread-safe) blocking the current thread.

        Args:
            timeout: Timeout in seconds.
            result: Whether the result should be returned (use `result=False` to avoid exceptions raised by [Pending.result][]).

        Raises:
            TimeoutError: When the timeout expires and a result or exception has not been set.
            PendingCancelled: If `result=True` and the pending has been cancelled.
            Exception: If `result=True` and an exception was set on the pending.

        Warning:
            **Blocking the thread in which the result or exception is set will cause in deadlock.**
        """
        if not self._done:
            done = Event()
            self._done_callbacks.appendleft(lambda _: done.set())
            if not self._done:
                done.wait(timeout)
            if not self._done:
                msg = f"Timeout waiting for {self}"
                raise TimeoutError(msg)
        else:
            green_checkpoint(force=True)
        return self.result() if result else None

    def _set_done(self, mode: Literal["result", "exception"], value) -> None:
        if self._done:
            raise InvalidStateError
        self._done = True
        setattr(self, "_" + mode, value)
        while self._done_callbacks:
            cb = self._done_callbacks.pop()
            try:
                cb(self)
            except Exception:
                pass

    def set_result(self, value: T, *, reset: bool = False) -> None:
        """
        Set the result (low-level-thread-safe).

        Args:
            value: The result.
            reset: Revert to being not done.

        Warning:
            - When using reset ensure to proivide sufficient time for any waiters to retrieve the result.
        """
        self._set_done("result", value)
        if reset:
            self._done = False

    def set_exception(self, exception: BaseException) -> None:
        """
        Set the exception (low-level-thread-safe).
        """
        self._set_done("exception", exception)

    @enable_signal_safety
    def cancel(self, msg: str | None = None) -> bool:
        """
        Cancel the instance.

        Args:
            msg: The message to use when cancelling.

        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.
        """
        if not self._done:
            if (cancelled := self._cancelled or "") and msg:
                msg = f"{cancelled}\n{msg}"
            self._cancelled = msg or cancelled
            if canceller := getattr(self, "_canceller", None):
                canceller(msg)
        return self.cancelled()

    def cancelled(self) -> bool:
        """Return True if the pending is cancelled."""
        return self._cancelled is not None

    def set_canceller(self, canceller: Callable[[str | None], Any]) -> None:
        """
        Set a callback to handle cancellation (low-level).

        Args:
            canceller: 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_result` must be called to mark the pending as completed.

        Example:
            ```python
            pen = Pending()
            pen.cancel()
            assert not pen.done()
            pen.set_canceller(lambda msg: pen.set_result(None))
            assert pen.done()
            ```
        """
        if self._done or hasattr(self, "_canceller"):
            raise InvalidStateError
        self._canceller = canceller
        if self.cancelled():
            self.cancel()

    def done(self) -> bool:
        """
        Returns True if a result or exception has been set.
        """
        return self._done

    def add_done_callback(self, fn: Callable[[Self], Any]) -> None:
        """
        Add a callback for when the pending is done (not thread-safe).

        If the pending is already done it will called immediately.
        """
        if not self._done:
            self._done_callbacks.append(fn)
        else:
            fn(self)

    def remove_done_callback(self, fn: Callable[[Self], object], /) -> int:
        """
        Remove all instances of a callback from the callbacks list.

        Returns the number of callbacks removed.
        """
        n = 0
        while fn in self._done_callbacks:
            n += 1
            self._done_callbacks.remove(fn)
        return n

    def result(self) -> T:
        """
        Return the result.

        Raises:
            PendingCancelled: If the pending has been cancelled.
            InvalidStateError: If the pending isn't done yet.
        """
        if e := self.exception():
            raise e from None
        try:
            return self._result
        except AttributeError:
            raise InvalidStateError from None

    def exception(self) -> BaseException | None:
        """
        Return the exception.

        Raises:
            PendingCancelled: If the instance has been cancelled.
        """
        if self._cancelled is not None:
            raise PendingCancelled(self._cancelled)
        return getattr(self, "_exception", None)

REPR_OMIT class-attribute

REPR_OMIT: set[str] = {'func', 'args', 'kwargs'}

Keys of metadata to omit when creating a repr of the instance.

metadata property

metadata: dict[str, Any]

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,).

__init__

__init__(
    trackers: type[PendingTracker] | tuple[type[PendingTracker], ...] = (),
    **metadata: Any,
)

Parameters:

  • trackers

    (type[PendingTracker] | tuple[type[PendingTracker], ...], default: () ) –

    A subclass or tuple of PendingTracker subclasses 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
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
def __init__(self, trackers: type[PendingTracker] | tuple[type[PendingTracker], ...] = (), **metadata: Any):
    """
    Initializes a new Pending object with optional creation options and metadata.

    Args:
        trackers: A subclass or tuple of `PendingTracker` subclasses to which the pending can be added given the context.
        **metadata: 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][]
    """
    self._done_callbacks: deque[Callable[[Self], Any]] = deque()
    self._metadata_mappings[id(self)] = metadata
    self._done = False
    self._cancelled = None
    self.trackers = trackers
    if trackers:
        PendingTracker.add_to_pending_trackers(self)

wait async

wait(
    *,
    timeout: float | None = ...,
    protect: bool = False | ...,
    result: Literal[True] = True,
) -> T
wait(*, timeout: float | None = ..., protect: bool = ..., result: Literal[False]) -> None
wait(
    *, timeout: float | None = None, protect: bool = False, result: bool = True
) -> T | None

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=False to avoid exceptions raised by Pending.result).

Raises:

  • TimeoutError

    When the timeout expires and a result or exception has not been set.

  • PendingCancelled

    If result=True and the pending has been cancelled.

  • Exception

    If result=True and 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
async def wait(self, *, timeout: float | None = None, protect: bool = False, result: bool = True) -> T | None:
    """
    Wait for a result or exception to be set (thread-safe) returning the pending if specified.

    Args:
        timeout: Timeout in seconds.
        protect: Protect the instance from external cancellation.
        result: Whether the result should be returned (use `result=False` to avoid exceptions raised by [Pending.result][]).

    Raises:
        TimeoutError: When the timeout expires and a result or exception has not been set.
        PendingCancelled: If `result=True` and the pending has been cancelled.
        Exception: If `result=True` and an exception was set on the pending.
    """
    try:
        if not self._done or self._done_callbacks:
            event = create_async_event()
            self._done_callbacks.appendleft(lambda _: event.set())
            with anyio.fail_after(timeout):
                if not self._done or self._done_callbacks:
                    await event
        else:
            await async_checkpoint(force=True)
        return self.result() if result else None
    except (anyio.get_cancelled_exc_class(), TimeoutError) as e:
        if not protect:
            self.cancel(f"Cancelled due to cancellation or timeout: {e}.")
        raise

wait_sync

wait_sync(*, timeout: float | None = ..., result: Literal[True] = True) -> T
wait_sync(*, timeout: float | None = ..., result: Literal[False]) -> None
wait_sync(*, timeout: float | None = None, result: bool = True) -> T | None

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=False to avoid exceptions raised by Pending.result).

Raises:

  • TimeoutError

    When the timeout expires and a result or exception has not been set.

  • PendingCancelled

    If result=True and the pending has been cancelled.

  • Exception

    If result=True and 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
def wait_sync(self, *, timeout: float | None = None, result: bool = True) -> T | None:
    """
    Wait synchronously for the a result or exception to be set (thread-safe) blocking the current thread.

    Args:
        timeout: Timeout in seconds.
        result: Whether the result should be returned (use `result=False` to avoid exceptions raised by [Pending.result][]).

    Raises:
        TimeoutError: When the timeout expires and a result or exception has not been set.
        PendingCancelled: If `result=True` and the pending has been cancelled.
        Exception: If `result=True` and an exception was set on the pending.

    Warning:
        **Blocking the thread in which the result or exception is set will cause in deadlock.**
    """
    if not self._done:
        done = Event()
        self._done_callbacks.appendleft(lambda _: done.set())
        if not self._done:
            done.wait(timeout)
        if not self._done:
            msg = f"Timeout waiting for {self}"
            raise TimeoutError(msg)
    else:
        green_checkpoint(force=True)
    return self.result() if result else None

set_result

set_result(value: T, *, reset: bool = False) -> None

Set the result (low-level-thread-safe).

Parameters:

  • value

    (T) –

    The result.

  • reset

    (bool, default: False ) –

    Revert to being not done.

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
def set_result(self, value: T, *, reset: bool = False) -> None:
    """
    Set the result (low-level-thread-safe).

    Args:
        value: The result.
        reset: Revert to being not done.

    Warning:
        - When using reset ensure to proivide sufficient time for any waiters to retrieve the result.
    """
    self._set_done("result", value)
    if reset:
        self._done = False

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
def set_exception(self, exception: BaseException) -> None:
    """
    Set the exception (low-level-thread-safe).
    """
    self._set_done("exception", exception)

cancel

cancel(msg: str | None = None) -> bool

Cancel the instance.

Parameters:

  • msg

    (str | None, default: None ) –

    The message to use when cancelling.

Notes

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
@enable_signal_safety
def cancel(self, msg: str | None = None) -> bool:
    """
    Cancel the instance.

    Args:
        msg: The message to use when cancelling.

    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.
    """
    if not self._done:
        if (cancelled := self._cancelled or "") and msg:
            msg = f"{cancelled}\n{msg}"
        self._cancelled = msg or cancelled
        if canceller := getattr(self, "_canceller", None):
            canceller(msg)
    return self.cancelled()

cancelled

cancelled() -> bool

Return True if the pending is cancelled.

Source code in src/async_kernel/pending.py
500
501
502
def cancelled(self) -> bool:
    """Return True if the pending is cancelled."""
    return self._cancelled is not None

set_canceller

set_canceller(canceller: Callable[[str | None], Any]) -> None

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_result must 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
def set_canceller(self, canceller: Callable[[str | None], Any]) -> None:
    """
    Set a callback to handle cancellation (low-level).

    Args:
        canceller: 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_result` must be called to mark the pending as completed.

    Example:
        ```python
        pen = Pending()
        pen.cancel()
        assert not pen.done()
        pen.set_canceller(lambda msg: pen.set_result(None))
        assert pen.done()
        ```
    """
    if self._done or hasattr(self, "_canceller"):
        raise InvalidStateError
    self._canceller = canceller
    if self.cancelled():
        self.cancel()

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
def done(self) -> bool:
    """
    Returns True if a result or exception has been set.
    """
    return self._done

add_done_callback

add_done_callback(fn: Callable[[Self], Any]) -> None

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
def add_done_callback(self, fn: Callable[[Self], Any]) -> None:
    """
    Add a callback for when the pending is done (not thread-safe).

    If the pending is already done it will called immediately.
    """
    if not self._done:
        self._done_callbacks.append(fn)
    else:
        fn(self)

remove_done_callback

remove_done_callback(fn: Callable[[Self], object]) -> int

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
def remove_done_callback(self, fn: Callable[[Self], object], /) -> int:
    """
    Remove all instances of a callback from the callbacks list.

    Returns the number of callbacks removed.
    """
    n = 0
    while fn in self._done_callbacks:
        n += 1
        self._done_callbacks.remove(fn)
    return n

result

result() -> T

Return the result.

Raises:

Source code in src/async_kernel/pending.py
560
561
562
563
564
565
566
567
568
569
570
571
572
573
def result(self) -> T:
    """
    Return the result.

    Raises:
        PendingCancelled: If the pending has been cancelled.
        InvalidStateError: If the pending isn't done yet.
    """
    if e := self.exception():
        raise e from None
    try:
        return self._result
    except AttributeError:
        raise InvalidStateError from None

exception

exception() -> BaseException | None

Return the exception.

Raises:

Source code in src/async_kernel/pending.py
575
576
577
578
579
580
581
582
583
584
def exception(self) -> BaseException | None:
    """
    Return the exception.

    Raises:
        PendingCancelled: If the instance has been cancelled.
    """
    if self._cancelled is not None:
        raise PendingCancelled(self._cancelled)
    return getattr(self, "_exception", None)