diff --git a/a_sync/config.py b/a_sync/config.py index bd54b37d..b23353f3 100644 --- a/a_sync/config.py +++ b/a_sync/config.py @@ -30,6 +30,12 @@ def get_default_executor() -> Executor: executor=default_sync_executor, ) +##################### +# Default Modifiers # +##################### + +# User configurable default modifiers to be applied to any a_sync decorated function if you do not specify kwarg values for each modifier. + DEFAULT_MODE: DefaultMode = os.environ.get("A_SYNC_DEFAULT_MODE") # type: ignore [assignment] CACHE_TYPE: CacheType = typ if (typ := os.environ.get("A_SYNC_CACHE_TYPE", "").lower()) else null_modifiers['cache_type'] CACHE_TYPED = bool(os.environ.get("A_SYNC_CACHE_TYPED")) @@ -39,7 +45,7 @@ def get_default_executor() -> Executor: RUNS_PER_MINUTE = rpm if (rpm := int(os.environ.get("A_SYNC_RUNS_PER_MINUTE", 0))) else null_modifiers['runs_per_minute'] SEMAPHORE = rpm if (rpm := int(os.environ.get("A_SYNC_SEMAPHORE", 0))) else null_modifiers['semaphore'] -default_modifiers = ModifierKwargs( +user_set_default_modifiers = ModifierKwargs( default=DEFAULT_MODE, cache_type=CACHE_TYPE, cache_typed=CACHE_TYPED, diff --git a/a_sync/decorator.py b/a_sync/decorator.py index 758d3324..91a0ec9b 100644 --- a/a_sync/decorator.py +++ b/a_sync/decorator.py @@ -132,12 +132,12 @@ def a_sync( # type: ignore [misc] lib defaults: async settings - cache_type: CacheType = None, - cache_typed: bool = False, - ram_cache_maxsize: Optional[int] = -1, - ram_cache_ttl: Optional[int] = None, - runs_per_minute: Optional[int] = None, - semaphore: SemaphoreSpec = semaphores.dummy_semaphore, + cache_type: CacheType = None, - This can be None or 'memory'. 'memory' is a lru cache which can be modified with the 'cache_typed','ram_cache_maxsize','ram_cache_ttl' modifiers. + cache_typed: bool = False, - Set to True if you want types considered treated for cache keys. ie with cache_typed=True, Decimal(0) and 0 will be considered separate keys. + ram_cache_maxsize: Optional[int] = -1, - The maxsize for your lru cache. None if cache is unbounded. If you set this value without specifying a cache type, 'memory' will automatically be applied. + ram_cache_ttl: Optional[int] = None, - The ttl for items in your lru cache. Set to None. If you set this value without specifying a cache type, 'memory' will automatically be applied. + runs_per_minute: Optional[int] = None, - Setting this value enables a rate limiter for the decorated function. + semaphore: SemaphoreSpec = None, - drop in a Semaphore for your async defined functions. sync settings executor: Executor = config.default_sync_executor diff --git a/a_sync/modifiers/manager.py b/a_sync/modifiers/manager.py index f0224515..524135d7 100644 --- a/a_sync/modifiers/manager.py +++ b/a_sync/modifiers/manager.py @@ -3,7 +3,7 @@ from a_sync import semaphores from a_sync._typing import * -from a_sync.config import default_modifiers, null_modifiers +from a_sync.config import user_set_default_modifiers, null_modifiers from a_sync.modifiers import cache, limiter valid_modifiers = [key for key in ModifierKwargs.__annotations__ if not key.startswith('_') and not key.endswith('_')] @@ -87,4 +87,4 @@ def __getitem__(self, modifier_key: str): return self._modifiers[modifier_key] # type: ignore [literal-required] nulls = ModifierManager(**null_modifiers) -user_defaults = ModifierManager(**default_modifiers) +user_defaults = ModifierManager(**user_set_default_modifiers)