Skip to content

Commit

Permalink
fix async
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Jul 15, 2024
1 parent abedf5e commit a0101bc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
18 changes: 13 additions & 5 deletions fastcore/xtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,26 +689,34 @@ def mk_dataclass(cls):
# %% ../nbs/03_xtras.ipynb 179
def flexicache(*funcs, maxsize=128):
"Like `lru_cache`, but customisable with policy `funcs`"
import asyncio
def _f(func):
cache,states = {}, [None]*len(funcs)
@wraps(func)
def wrapper(*args, **kwargs):
key = f"{args} // {kwargs}"
def _cache_logic(key, execute_func):
if key in cache:
result,states = cache[key]
if not any(f(state) for f,state in zip(funcs, states)):
cache[key] = cache.pop(key)
return result
del cache[key]
try: newres = func(*args, **kwargs)
try: newres = execute_func()
except:
if key not in cache: raise
cache[key] = cache.pop(key)
return result
cache[key] = (newres, [f(None) for f in funcs])
if len(cache) > maxsize: cache.popitem()
return newres
return wrapper

@wraps(func)
def wrapper(*args, **kwargs):
return _cache_logic(f"{args} // {kwargs}", lambda: func(*args, **kwargs))

@wraps(func)
async def async_wrapper(*args, **kwargs):
return await _cache_logic(f"{args} // {kwargs}", lambda: asyncio.ensure_future(func(*args, **kwargs)))

return async_wrapper if asyncio.iscoroutinefunction(func) else wrapper
return _f

# %% ../nbs/03_xtras.ipynb 181
Expand Down
41 changes: 33 additions & 8 deletions nbs/03_xtras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2849,26 +2849,34 @@
"#| export\n",
"def flexicache(*funcs, maxsize=128):\n",
" \"Like `lru_cache`, but customisable with policy `funcs`\"\n",
" import asyncio\n",
" def _f(func):\n",
" cache,states = {}, [None]*len(funcs)\n",
" @wraps(func)\n",
" def wrapper(*args, **kwargs):\n",
" key = f\"{args} // {kwargs}\"\n",
" def _cache_logic(key, execute_func):\n",
" if key in cache:\n",
" result,states = cache[key]\n",
" if not any(f(state) for f,state in zip(funcs, states)):\n",
" cache[key] = cache.pop(key)\n",
" return result\n",
" del cache[key]\n",
" try: newres = func(*args, **kwargs)\n",
" try: newres = execute_func()\n",
" except:\n",
" if key not in cache: raise\n",
" cache[key] = cache.pop(key)\n",
" return result\n",
" cache[key] = (newres, [f(None) for f in funcs])\n",
" if len(cache) > maxsize: cache.popitem()\n",
" return newres\n",
" return wrapper\n",
"\n",
" @wraps(func)\n",
" def wrapper(*args, **kwargs):\n",
" return _cache_logic(f\"{args} // {kwargs}\", lambda: func(*args, **kwargs))\n",
"\n",
" @wraps(func)\n",
" async def async_wrapper(*args, **kwargs):\n",
" return await _cache_logic(f\"{args} // {kwargs}\", lambda: asyncio.ensure_future(func(*args, **kwargs)))\n",
"\n",
" return async_wrapper if asyncio.iscoroutinefunction(func) else wrapper\n",
" return _f"
]
},
Expand Down Expand Up @@ -2913,10 +2921,23 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@flexicache(time_policy(10), mtime_policy('000_tour.ipynb'))\n",
"def cached_func(x, y): return x+y"
"def cached_func(x, y): return x+y\n",
"\n",
"cached_func(1,2)"
]
},
{
Expand All @@ -2936,7 +2957,11 @@
}
],
"source": [
"cached_func(1,2)"
"@flexicache(time_policy(10), mtime_policy('000_tour.ipynb'))\n",
"async def cached_func(x, y): return x+y\n",
"\n",
"await cached_func(1,2)\n",
"await cached_func(1,2)"
]
},
{
Expand Down

0 comments on commit a0101bc

Please sign in to comment.