asyncutils.context

Contents

asyncutils.context#

Contextual configuration system, inspired by the decimal module.

Attributes#

all_contextual_consts

A frozenset of all contextual constant names, for use in validating that only valid contextual constants are accessed or modified.

Classes#

Context

localcontext

Context manager that temporarily sets the context of the current thread to a modified version of the provided context. Non-reentrant, but reusable with the exact same new_ctx.

nonreusablelocalcontext

Version of localcontext that is not reusable. Use this to avoid subtle bugs, especially since it's not that expensive to instantiate a Context.

Functions#

getcontext(→ Context)

Return the current context for the active thread.

setcontext(→ None)

Set the current context to for the active thread to ctx.

Module Contents#

class asyncutils.context.Context[source]#
An object storing configuration for various functions and patterns in this library, for immutability and performance; that is, not loading dataclasses for
@dataclass, which loads inspect, triggering a cascade of imports.
collections.namedtuple() is also unsuitable for this use case, since it behaves like a sequence.
The order of the fields are kept in alphabetical order of submodule and in each submodule, and new fields may be added in the future.
For consistency, each field is named in all caps with words separated by underscores, and prefixed by the name of the utility it is used in, followed by a concise description of what it configures.

Tip

If you need to use any of the settings, you can find the documentation under the API reference for the utilities that use that setting.

Note

Refer to config for the factory default values of each setting.

Note

It is possible, but discouraged, to access these fields with attribute names that are not all uppercase.

Note

This is only type annotated as a dataclass for convenience and to avoid even more code duplication than there is now.

__copy__() Self#

Alias for copy().

__getitem__(name: str, /) Any[source]#

Context’s also behave like mutable mappings.

__setitem__(name: str, value: Any, /) None#

Alias for __setattr__().

ascurctx() nonreusablelocalcontext[source]#

Return a non-reusable context manager that sets the context to this context on entry. ctx.ascurctx() is syntactic sugar for nonreusablelocalcontext(ctx)

asdict() dict[str, Any][source]#

Return a dictionary representing the items within the context.

copy() Self[source]#

Return a shallow copy of the context.

classmethod from_dct(dct: dict[str, Any], /) Self[source]#

Build an instance from the keys of the dictionary.

pprint(file: asyncutils._internal.types.CanWriteAndFlush[str] = ..., *, pp: pprint.PrettyPrinter = ..., incl_newline: bool = ...) None[source]#

Pretty print the context to the provided file-like object file with the pprint.PrettyPrinter instance pp, without a trailing newline if incl_newline=False is specified.

replace(/, **k: Any) Self[source]#

Return a new instance with the same values as this one besides the keyword arguments.

replace_from_dct(dct: dict[str, Any], /) Self[source]#

Return a new instance with the same values as this one besides the keys of dct.

update(dct: dict[str, Any] = ..., /, **k: Any) None[source]#

Update the values of the instance with dct if passed, then the keyword arguments.

ADVANCED_POOL_DEFAULT_MAX_WORKERS: int = Ellipsis#
ADVANCED_POOL_DEFAULT_MIN_WORKERS: int = Ellipsis#
ADVANCED_POOL_FACTOR: float = Ellipsis#
ADVANCED_POOL_THRESHOLD_HI: float = Ellipsis#
ADVANCED_POOL_THRESHOLD_LO: float = Ellipsis#
ADVANCED_RATE_LIMIT_DEFAULT_TOKENS: float = Ellipsis#
AFRIEVALDS_DEFAULT_K: int = Ellipsis#
AITER_TO_GEN_DEFAULT_ALLOW_FUTURES: bool = Ellipsis#
AITER_TO_GEN_DEFAULT_STRICT: bool = Ellipsis#
AONLINESORTER_DEFAULT_SLOW: bool = Ellipsis#
ASYNC_LRU_CACHE_DEFAULT_MAX_SIZE: int = Ellipsis#
AUNZIP_DEFAULT_MAX_QSIZE: int = Ellipsis#
AUNZIP_DEFAULT_PUT_BATCH: int = Ellipsis#
BACKGROUND_REFRESH_CACHE_DEFAULT_REFRESH: float = Ellipsis#
BACKGROUND_REFRESH_CACHE_DEFAULT_TTL: float = Ellipsis#
BATCH_PROCESSOR_DEFAULT_MAX_SIZE: int = Ellipsis#
BATCH_PROCESSOR_DEFAULT_MAX_TIME: float = Ellipsis#
BENCHMARK_DEFAULT_SEQUENTIAL: bool = Ellipsis#
BENCHMARK_DEFAULT_TIMES: int = Ellipsis#
BENCHMARK_DEFAULT_WARMUP: int = Ellipsis#
BOUNDED_BATCH_PROCESSOR_DEFAULT_BATCH_SIZE: int = Ellipsis#
BOUNDED_BATCH_PROCESSOR_DEFAULT_MAX_CONCURRENT: int = Ellipsis#
BULKHEAD_DEFAULT_MAX_QUEUE: int = Ellipsis#
BULKHEAD_DEFAULT_MAX_REJ: int = Ellipsis#
CIRCUIT_BREAKER_DEFAULT_MAX_FAILS: int = Ellipsis#
CIRCUIT_BREAKER_DEFAULT_MAX_HALF_OPEN_CALLS: int = Ellipsis#
CIRCUIT_BREAKER_DEFAULT_RESET: float = Ellipsis#
CONNECTION_POOL_DEFAULT_MAX_LIFE: float = Ellipsis#
CONNECTION_POOL_DEFAULT_MAX_SIZE: int = Ellipsis#
CONNECTION_POOL_DEFAULT_MIN_SIZE: int = Ellipsis#
CONNECTION_POOL_MAINTENANCE_INTERVAL: float = Ellipsis#
CONVERT_TO_CORO_ITER_DEFAULT_SKIP_INVALID: bool = Ellipsis#
DUAL_CONTEXT_MANAGER_DEFAULT_MAY_CREATE_EXECUTOR: bool = Ellipsis#
DUAL_CONTEXT_MANAGER_DEFAULT_STRICT: bool = Ellipsis#
DUAL_CONTEXT_MANAGER_DEFAULT_USE_EXISTING_EXECUTOR: bool = Ellipsis#
DYNAMIC_BOUNDED_SEMAPHORE_DEFAULT_VALUE: int = Ellipsis#
DYNAMIC_THROTTLE_DEFAULT_JITTER: float = Ellipsis#
DYNAMIC_THROTTLE_DEFAULT_LBOUND: float = Ellipsis#
DYNAMIC_THROTTLE_DEFAULT_LFACTOR: float = Ellipsis#
DYNAMIC_THROTTLE_DEFAULT_MAX_RATE: float = Ellipsis#
DYNAMIC_THROTTLE_DEFAULT_MIN_RATE: float = Ellipsis#
DYNAMIC_THROTTLE_DEFAULT_UBOUND: float = Ellipsis#
DYNAMIC_THROTTLE_DEFAULT_UFACTOR: float = Ellipsis#
DYNAMIC_THROTTLE_DEFAULT_WINDOW: int = Ellipsis#
EVENT_BUS_DEFAULT_MAX_CONCURRENT: int = Ellipsis#
EVENT_BUS_PUBLISH_DEFAULT_SAFE: bool = Ellipsis#
EVENT_BUS_STREAM_DEFAULT_BUFFER_SIZE: int = Ellipsis#
EVENT_BUS_STREAM_DEFAULT_ITEM_TIMEOUT: float | None = Ellipsis#
EVENT_BUS_STREAM_DEFAULT_TIMEOUT: float | None = Ellipsis#
EVENT_LOOP_BASE_FLAGS: int = Ellipsis#
EVENT_WITH_VALUE_DEFAULT_MAX_HIST: int = Ellipsis#
EVENT_WITH_VALUE_DEFAULT_RECENT: float = Ellipsis#
GATHER_WITH_LIMITED_CONCURRENCY_DEFAULT_MAX_CONCURRENT: int = Ellipsis#
ITER_TO_AGEN_DEFAULT_MAY_CREATE_EXECUTOR: bool = Ellipsis#
ITER_TO_AGEN_DEFAULT_STRICT: bool = Ellipsis#
ITER_TO_AGEN_DEFAULT_USE_EXISTING_EXECUTOR: bool = Ellipsis#
LEAKY_BUCKET_ADJMAP: _collections_abc.Sequence[tuple[float, tuple[float, float, float, float]]] = Ellipsis#
LEAKY_BUCKET_DEFAULT_ACQUIRE_TOKENS: float = Ellipsis#
LEAKY_BUCKET_DEFAULT_EXT_CAN_SET_FACTOR: bool = Ellipsis#
LEAKY_BUCKET_DEFAULT_MAX_FACTOR: float = Ellipsis#
LEAKY_BUCKET_DEFAULT_MIN_FACTOR: float = Ellipsis#
LEAKY_BUCKET_DEFAULT_WAIT_FOR_TOKENS_TOKENS: float = Ellipsis#
LEAKY_BUCKET_WAIT_FOR_TOKENS_TICK: float = Ellipsis#
LINE_PROTOCOL_DEFAULT_BUFFER_SIZE: int = Ellipsis#
LOCKSMITH_DEFAULT_TIMEOUTS: tuple[float | None, float | None, float | None] = Ellipsis#
MEMORY_MAPPED_IO_MANAGER_DEFAULT_CHECKSUM_ALG: asyncutils._internal.types.HashAlgorithm = Ellipsis#
MEMORY_MAPPED_IO_MANAGER_DEFAULT_MINIMIZE_WRITES: bool = Ellipsis#
MERGE_DEFAULT_MAX_QSIZE: int = Ellipsis#
OBSERVABLE_DEFAULT_NTIMES_N: int = Ellipsis#
PASSWORD_QUEUE_DEFAULT_GET_FROM: str = Ellipsis#
PASSWORD_QUEUE_DEFAULT_PUT_FROM: str = Ellipsis#
PRIORITY_SEMAPHORE_DEFAULT_VALUE: int = Ellipsis#
RENDEZVOUS_MAINTENANCE_INTERVAL: float = Ellipsis#
RETRY_DEFAULT_BACKOFF: float = Ellipsis#
RETRY_DEFAULT_DELAY: float = Ellipsis#
RETRY_DEFAULT_JITTER: float = Ellipsis#
RETRY_DEFAULT_MAX_DELAY: float = Ellipsis#
RETRY_DEFAULT_TRIES: int = Ellipsis#
RWLOCK_DEFAULT_PREFER_WRITERS: bool = Ellipsis#
SEMAPHORE_DEFAULT_VALUE: int = Ellipsis#
SOCKET_TRANSPORT_LIMITS: tuple[int, int] = Ellipsis#
TEE_DEFAULT_MAX_QSIZE: int = Ellipsis#
TEE_DEFAULT_PUT_EXC: bool = Ellipsis#
TIMER_DEFAULT_PRECISION: int = Ellipsis#
TOKEN_BUCKET_DEFAULT_CONSUME_TOKENS: float = Ellipsis#
WAIT_FOR_SIGNAL_DEFAULT_SIGNALS: _collections_abc.Sequence[int] = Ellipsis#
class asyncutils.context.localcontext(ctx: Context = ..., **k: Any)[source]#

Context manager that temporarily sets the context of the current thread to a modified version of the provided context. Non-reentrant, but reusable with the exact same new_ctx.

Note that the context of the current thread is to be set to a shallow copy of ctx, defaulting to the current context, with replacements from the keyword arguments.

async __aenter__() Context[source]#

Return the new context after setting it.

async __aexit__(exc_typ: asyncutils._internal.types.ExcType, exc_val: BaseException, exc_tb: types.TracebackType, /) None[source]#
async __aexit__(exc_typ: None, exc_val: None, exc_tb: None, /) None

Reset the context to the previous.

__enter__() Context[source]#

Return the new context after setting it.

__exit__(exc_typ: asyncutils._internal.types.ExcType, exc_val: BaseException, exc_tb: types.TracebackType, /) None[source]#
__exit__(exc_typ: None, exc_val: None, exc_tb: None, /) None

Reset the context to the previous.

property new_ctx: Context#

The new context to be set on context manager entry.

property saved_ctx: Context#

The previous context to be restored on context manager exit.

class asyncutils.context.nonreusablelocalcontext(ctx: Context = ..., **k: Any)[source]#

Bases: localcontext

Version of localcontext that is not reusable. Use this to avoid subtle bugs, especially since it’s not that expensive to instantiate a Context.

Note that the context of the current thread is to be set to a shallow copy of ctx, defaulting to the current context, with replacements from the keyword arguments.

asyncutils.context.getcontext() Context[source]#

Return the current context for the active thread.

asyncutils.context.setcontext(ctx: Context, /) None[source]#

Set the current context to for the active thread to ctx.

asyncutils.context.all_contextual_consts: frozenset[str]#

A frozenset of all contextual constant names, for use in validating that only valid contextual constants are accessed or modified.

Note

These names are not listed by calling dir() on this submodule, since there are so many of them (86 as of now!) and more may be added in the future, and the recommended way to get their values is to query them on the actual context object anyway. They are still provided below to facilitate type checking.