You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi folks,
I really love your module and we even use it at work quit a lot :D
I wanted to contribute and add support for async methods but got stuck since I cannot really access the cache store behind the cached decorator...
here's the basic approach:
importasynciofromtimeimportperf_counterfromfunctoolsimportwrapsdeflog(msg: str):
print(f"[{round(perf_counter(),3)}]: {msg}")
classAsyncMemoize:
def__init__(self) ->None:
self.cache= {} # very silly store for proof of conceptdef__call__(self, function):
@wraps(function)asyncdefwrapped(*args):
cache_key=hash(args) # ifself.cache.get(cache_key, ...) == ...:
self.cache[cache_key] =awaitfunction(*args)
returnself.cache[cache_key]
returnwrapped@AsyncMemoize()asyncdefcalculate_stuff(n: int):
awaitasyncio.sleep(1) # intensive calculationreturnnasyncdefmain():
log("Calculating...")
awaitcalculate_stuff(4)
log("Calculating again...")
awaitcalculate_stuff(4)
log("Calculating other...")
awaitcalculate_stuff(7)
log("Calculating other again...")
awaitcalculate_stuff(7)
log("All done!")
asyncio.run(main())
I could work with the cached decorator, however, this would mean making an async function call sync, and therefore blocking the thread, which is complete bullshit of cause 😆
If you can show me how to access the store directly, I'm gonna fork this repo and implement async support (which is really nice for things like server-side caching on Quart and FastAPI etc.) 😀
Best regards 👋
The text was updated successfully, but these errors were encountered:
Hi folks,
I really love your module and we even use it at work quit a lot :D
I wanted to contribute and add support for async methods but got stuck since I cannot really access the cache store behind the
cached
decorator...here's the basic approach:
I could work with the
cached
decorator, however, this would mean making an async function call sync, and therefore blocking the thread, which is complete bullshit of cause 😆If you can show me how to access the store directly, I'm gonna fork this repo and implement async support (which is really nice for things like server-side caching on Quart and FastAPI etc.) 😀
Best regards 👋
The text was updated successfully, but these errors were encountered: