-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big sloppy commit - some feats, some fixes, some refactorings (#17)
* feat: wrap sync and async bound methods on ASyncABC subclasses, allow modifier overriding * feat: lru cache * feat: env vars to set system wide defaults * feat: improve type hint accuracy * fix: read modifiers from class def * fix: semaphore * fix: missing async_lru dep * fix: default as first arg for property and cached property * chore: rename Modifiers -> ModifierManager and cleanup imports
- Loading branch information
1 parent
9d81bbf
commit 3b1c82e
Showing
17 changed files
with
500 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
import asyncio | ||
from concurrent.futures._base import Executor | ||
from typing import (Awaitable, Callable, Generic, Literal, Optional, TypedDict, | ||
TypeVar, Union, overload) | ||
|
||
from typing_extensions import ParamSpec, Unpack | ||
|
||
T = TypeVar("T") | ||
P = ParamSpec("P") | ||
|
||
class ModifierKwargs(TypedDict, total=False): | ||
default: Literal['sync', 'async', None] | ||
cache_type: Literal['memory', None] | ||
cache_typed: bool | ||
ram_cache_maxsize: Optional[int] | ||
ram_cache_ttl: Optional[int] | ||
runs_per_minute: int | ||
semaphore: Union[int, asyncio.Semaphore] | ||
# sync modifiers | ||
executor: Executor | ||
|
||
class Modified(Generic[T]): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,56 @@ | ||
|
||
import functools | ||
import os | ||
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor | ||
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor | ||
from concurrent.futures._base import Executor | ||
|
||
from a_sync._typing import * | ||
from a_sync.modifiers import ModifierManager | ||
|
||
EXECUTOR_TYPE = os.environ.get("A_SYNC_EXECUTOR_TYPE", "threads") | ||
EXECUTOR_VALUE = int(os.environ.get("A_SYNC_EXECUTOR_VALUE", 8)) | ||
|
||
default_sync_executor: Executor | ||
if EXECUTOR_TYPE.lower().startswith('p'): # p, P, proc, Processes, etc | ||
default_sync_executor = ProcessPoolExecutor(EXECUTOR_VALUE) | ||
elif EXECUTOR_TYPE.lower().startswith('t'): # t, T, thread, THREADS, etc | ||
default_sync_executor = ThreadPoolExecutor(EXECUTOR_VALUE) | ||
else: | ||
@functools.lru_cache(maxsize=1) | ||
def get_default_executor() -> Executor: | ||
if EXECUTOR_TYPE.lower().startswith('p'): # p, P, proc, Processes, etc | ||
return ProcessPoolExecutor(EXECUTOR_VALUE) | ||
elif EXECUTOR_TYPE.lower().startswith('t'): # t, T, thread, THREADS, etc | ||
return ThreadPoolExecutor(EXECUTOR_VALUE) | ||
raise ValueError("Invalid value for A_SYNC_EXECUTOR_TYPE. Please use 'threads' or 'processes'.") | ||
|
||
default_sync_executor = get_default_executor() | ||
|
||
null_modifiers = ModifierManager( | ||
ModifierKwargs( | ||
default=None, | ||
cache_type=None, | ||
cache_typed=False, | ||
ram_cache_maxsize=-1, | ||
ram_cache_ttl=None, | ||
runs_per_minute=None, | ||
semaphore=None, | ||
executor=default_sync_executor, | ||
) | ||
) | ||
|
||
DEFAULT_MODE = os.environ.get("A_SYNC_DEFAULT_MODE") | ||
CACHE_TYPE = 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")) | ||
RAM_CACHE_MAXSIZE = int(os.environ.get("A_SYNC_RAM_CACHE_MAXSIZE", -1)) | ||
RAM_CACHE_TTL = ttl if (ttl := float(os.environ.get("A_SYNC_RAM_CACHE_TTL", 0))) else null_modifiers.ram_cache_ttl | ||
|
||
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 = ModifierManager( | ||
ModifierKwargs( | ||
default=DEFAULT_MODE, | ||
cache_type=CACHE_TYPE, | ||
cache_typed=CACHE_TYPED, | ||
ram_cache_maxsize=RAM_CACHE_MAXSIZE, | ||
ram_cache_ttl=RAM_CACHE_TTL, | ||
runs_per_minute=RUNS_PER_MINUTE, | ||
semaphore=SEMAPHORE, | ||
executor=default_sync_executor, | ||
) | ||
) |
Oops, something went wrong.