Make one with ScopedRef.make or ScopedRef.from_acquire inside
a scope.
Functions#
__init__#
__init__(scope, generation, value) -> None| Parameter | Type | Description |
|---|---|---|
scope | CloseableScope | |
generation | CloseableScope | |
value | A |
def __init__(self, scope: CloseableScope, generation: CloseableScope, value: A) -> None:
self._scope = scope
self._current = (generation, value)
self._lock = Semaphore(1)from_acquire#
from_acquire(acquire) -> Effect[ScopedRef[T], X, Scope | R]A ref holding what acquire gives, with its resources in the
ref's first generation.
| Parameter | Type | Description |
|---|---|---|
acquire | Effect[T, X, R] |
Returns Effect[ScopedRef[T], X, Scope | R]
@staticmethod
def from_acquire[T, X = Never, R = Never](
acquire: Effect[T, X, R],
) -> Effect[ScopedRef[T], X, Scope | R]:
"""A ref holding what ``acquire`` gives, with its resources in the
ref's first generation."""
require_effect(acquire, "acquire")
def body():
owner = yield from Scope()
scope = owner._fork_now()
generation = scope._fork_now()
value: T = yield from _in_scope(acquire, generation).tap_cause(
lambda cause: scope.close(Failure(cause))
)
if generation._closed is not None:
return (yield from interrupt())
return ScopedRef(scope, generation, value)
return _Gen(body).uninterruptible()make#
make(evaluate) -> Effect[ScopedRef[T], Never, Scope]A ref holding evaluate(), which has no resources.
| Parameter | Type | Description |
|---|---|---|
evaluate | Callable[[], T] |
Returns Effect[ScopedRef[T], Never, Scope]
@staticmethod
def make[T](evaluate: Callable[[], T]) -> Effect[ScopedRef[T], Never, Scope]:
"""A ref holding ``evaluate()``, which has no resources."""
return ScopedRef.from_acquire(sync_effect(evaluate))unsafe_get#
unsafe_get() -> AReturns A
def unsafe_get(self) -> A:
return self._current[1]get#
get() -> Effect[A]Returns Effect[A]
def get(self) -> Effect[A]:
return _Sync(self.unsafe_get)set#
set(acquire) -> Effect[None, Any, Any]Acquire a new value in a new generation, then release the old one. Sets run one at a time. If acquiring fails the old value stays; if releasing the old value fails, the new one is released too and the old one stays current.
| Parameter | Type | Description |
|---|---|---|
acquire | Effect[A, Any, Any] |
Returns Effect[None, Any, Any]
def set(self, acquire: Effect[A, Any, Any]) -> Effect[None, Any, Any]:
"""Acquire a new value in a new generation, then release the old
one. Sets run one at a time. If acquiring fails the old value
stays; if releasing the old value fails, the new one is released
too and the old one stays current."""
require_effect(acquire, "acquire")
def body():
generation = self._scope._fork_now()
def discard(cause: Any) -> Effect[None]:
return generation.close(Failure(cause))
value = yield from _in_scope(acquire, generation).tap_cause(discard)
yield from self._current[0].close(_DONE).tap_cause(discard)
if generation._closed is not None:
return (yield from interrupt())
self._current = (generation, value)
return self._lock.with_permits(_Gen(body).uninterruptible())