GitHub

Deferred

API reference for Deferred.

Functions

__init__

__init__() -> None
def __init__(self) -> None:
    self._effect: Effect[A, E] | None = None
    self._waiters: list[Callable[[Effect[A, E]], None]] = []

make

make() -> Effect[Deferred[A, E]]

Deferred[int, MyError].make()

Returns Effect[Deferred[A, E]]

@classmethod
def make(cls) -> Effect[Deferred[A, E]]:
    """``Deferred[int, MyError].make()``"""
    return _Sync(cls)

wait

wait() -> Effect[A, E]

Wait for the result and take it on.

Returns Effect[A, E]

def wait(self) -> Effect[A, E]:
    """Wait for the result and take it on."""

    def register(resume: Callable[[Effect[A, E]], None]) -> Effect[None] | None:
        if self._effect is not None:
            resume(self._effect)
            return None
        self._waiters.append(resume)
        return _Sync(lambda: self._waiters.remove(resume) if resume in self._waiters else None)

    return _Callback(register)

done

done(ex) -> Effect[bool]
ParameterTypeDescription
exExit[A, E]

Returns Effect[bool]

def done(self, ex: Exit[A, E]) -> Effect[bool]:
    return _Sync(lambda: self.unsafe_done(ex))

succeed

succeed(value) -> Effect[bool]
ParameterTypeDescription
valueA

Returns Effect[bool]

def succeed(self, value: A) -> Effect[bool]:
    return self.done(Success(value))

fail

fail(error) -> Effect[bool]
ParameterTypeDescription
errorE

Returns Effect[bool]

def fail(self, error: E) -> Effect[bool]:
    return self.done(fail_exit(error))

fail_cause

fail_cause(cause) -> Effect[bool]
ParameterTypeDescription
causeCause[E]

Returns Effect[bool]

def fail_cause(self, cause: Cause[E]) -> Effect[bool]:
    return self.done(Failure(cause))

sync

sync(evaluate) -> Effect[bool]

Succeed with evaluate(), computed when this runs.

ParameterTypeDescription
evaluateCallable[[], A]

Returns Effect[bool]

def sync(self, evaluate: Callable[[], A]) -> Effect[bool]:
    """Succeed with ``evaluate()``, computed when this runs."""
    return _Suspend(lambda: self.succeed(evaluate()))

fail_sync

fail_sync(evaluate) -> Effect[bool]
ParameterTypeDescription
evaluateCallable[[], E]

Returns Effect[bool]

def fail_sync(self, evaluate: Callable[[], E]) -> Effect[bool]:
    return _Suspend(lambda: self.fail(evaluate()))

fail_cause_sync

fail_cause_sync(evaluate) -> Effect[bool]
ParameterTypeDescription
evaluateCallable[[], Cause[E]]

Returns Effect[bool]

def fail_cause_sync(self, evaluate: Callable[[], Cause[E]]) -> Effect[bool]:
    return _Suspend(lambda: self.fail_cause(evaluate()))

die

die(defect) -> Effect[bool]
ParameterTypeDescription
defectBaseException

Returns Effect[bool]

def die(self, defect: BaseException) -> Effect[bool]:
    return self.done(die_exit(defect))

die_sync

die_sync(evaluate) -> Effect[bool]
ParameterTypeDescription
evaluateCallable[[], BaseException]

Returns Effect[bool]

def die_sync(self, evaluate: Callable[[], BaseException]) -> Effect[bool]:
    return _Suspend(lambda: self.die(evaluate()))

interrupt

interrupt() -> Effect[bool]

Complete with an interrupt by the current fiber.

Returns Effect[bool]

def interrupt(self) -> Effect[bool]:
    """Complete with an interrupt by the current fiber."""
    return _WithFiber(lambda fiber: self.interrupt_with(fiber.id))

interrupt_with

interrupt_with(fiber_id) -> Effect[bool]

Complete with an interrupt by the fiber with id fiber_id.

ParameterTypeDescription
fiber_idint

Returns Effect[bool]

def interrupt_with(self, fiber_id: int) -> Effect[bool]:
    """Complete with an interrupt by the fiber with id ``fiber_id``."""
    return self.done(interrupt_exit(fiber_id))

complete

complete(eff) -> Effect[bool, Never, R]

Run eff and complete with its outcome. Does not run it if already completed; an interrupt while it runs completes with that interrupt.

ParameterTypeDescription
effEffect[A, E, R]

Returns Effect[bool, Never, R]

def complete[R](self, eff: Effect[A, E, R]) -> Effect[bool, Never, R]:
    """Run ``eff`` and complete with its outcome. Does not run it if
    already completed; an interrupt while it runs completes with that
    interrupt."""
    require_effect(eff, "eff")
    return _Suspend(lambda: _Succeed(False) if self._effect is not None else self.into(eff))

into

into(eff) -> Effect[bool, Never, R]

Run eff and complete with its outcome, even if the running fiber is interrupted (then with that interrupt). Runs eff even when already completed; complete does not.

ParameterTypeDescription
effEffect[A, E, R]

Returns Effect[bool, Never, R]

def into[R](self, eff: Effect[A, E, R]) -> Effect[bool, Never, R]:
    """Run ``eff`` and complete with its outcome, even if the running
    fiber is interrupted (then with that interrupt). Runs ``eff`` even
    when already completed; ``complete`` does not."""
    require_effect(eff, "eff")
    return uninterruptible_mask(lambda restore: restore(eff).exit().flat_map(self.done))

complete_with

complete_with(eff) -> Effect[bool]

Complete with eff itself, unrun: every wait() runs it.

ParameterTypeDescription
effEffect[A, E]

Returns Effect[bool]

def complete_with(self, eff: Effect[A, E]) -> Effect[bool]:
    """Complete with ``eff`` itself, unrun: every ``wait()`` runs it."""
    require_effect(eff, "eff")
    return _Sync(lambda: self._settle(eff))

is_done

is_done() -> Effect[bool]

Returns Effect[bool]

def is_done(self) -> Effect[bool]:
    return _Sync(self.unsafe_is_done)

unsafe_is_done

unsafe_is_done() -> bool

Returns bool

def unsafe_is_done(self) -> bool:
    return self._effect is not None

poll

poll() -> Effect[Effect[A, E] | None]

The effect waiters will run, or None if not completed yet.

Returns Effect[Effect[A, E] | None]

def poll(self) -> Effect[Effect[A, E] | None]:
    """The effect waiters will run, or ``None`` if not completed yet."""
    return _Suspend(lambda: _Succeed(self._effect))

unsafe_done

unsafe_done(ex) -> bool

Complete from outside an effect. Call on the fiber's scheduler thread (e.g. inside sync).

ParameterTypeDescription
exExit[A, E]

Returns bool

def unsafe_done(self, ex: Exit[A, E]) -> bool:
    """Complete from outside an effect. Call on the fiber's scheduler
    thread (e.g. inside ``sync``)."""
    return self._settle(_FromExit(ex))