JanS
1
Hi all,
I read about automatic caching in Panel described here: Automatically Cache — Panel v1.4.1 (holoviz.org)
In my example below multiple decorated functions are involved, and get_data_1.clear()
seems to clear the cache of get_data_2
.
Any hints whats going on here?
Thanks a lot
from datetime import datetime
import panel as pn
@pn.cache()
def get_data_1():
return datetime.now()
@pn.cache()
def get_data_2():
return datetime.now()
md = pn.pane.Markdown()
def get_data():
get_data_1.clear()
md.object = (f"1: {get_data_1()} (Value should change as cache was cleared...)\n"
f"2: {get_data_2()} (Value should NOT change as no cache was cleared...)")
button = pn.widgets.Button()
button.on_click(lambda _: get_data())
pn.serve(pn.Column(md, button))
Marc
2
Hi @JanS
Welcome to the community.
This is a bug. I have reported it in cache .clear not clearing another function · Issue #6777 · holoviz/panel (github.com).
Until its been fixed and relased you can use the custom clear
function below.
from datetime import datetime
import panel as pn
import hashlib
import sys
from panel.io.cache import _generate_hash
def get_func_hash(func):
func_name = func.__name__
module = sys.modules[func.__module__]
fname = '__main__' if func.__module__ == '__main__' else module.__file__
func_hash = (fname, func_name)
return hashlib.sha256(_generate_hash(func_hash)).hexdigest()
def clear(func):
func_hash = get_func_hash(func)
cache = pn.state._memoize_cache.get(func_hash, {})
cache.clear()
# Example usage:
def example_function(x, y):
return x + y
@pn.cache()
def get_data_1():
return datetime.now()
@pn.cache()
def get_data_2():
return datetime.now()
md = pn.pane.Markdown()
def get_data():
clear(get_data_1)
md.object = (f"1: {get_data_1()} (Value should change as cache was cleared...)\n"
f"2: {get_data_2()} (Value should NOT change as no cache was cleared...)")
button = pn.widgets.Button(name="Update")
button.on_click(lambda _: get_data())
pn.Column(md, button).servable()
JanS
3
@Marc Thank you & also for all your effort, highly appreciated!! Your new tutorial is very helpful!
Panel is really, really great!!!
1 Like
Marc
4
Thx. Panel 1.4.2 has been released fixing the cache .clear
issue.