for the cache_key_fn parameter, what is the differ...
# ask-community
h
for the cache_key_fn parameter, what is the difference between using
always
vs
lambda: "always"
Copy code
@task(cache_key_fn=lambda: "always", cache_expiration=timedelta(seconds=20))
def my_task():
      return "something"
d
This is more of a python question. But I hope this helps.
Copy code
x="always"
y=lambda : "always"
print(x)
print(y)
print(y())
Running this gives the output.
Copy code
always
<function <lambda> at 0x7f324e6c7d90>
always
Using lambda returns callable function that returns the string.
h
thank you for taking the time to respond. Curious as to why input lambda, a callable function as a parameter rather than a string.
d
You can write your own functions that generates name for the task run. something like https://github.com/PrefectHQ/prefect/blob/d75c2fb1d57a594195d054f109d3c0d8dd44e81d/src/prefect/tasks.py#L59
^Wrong - but the concept is similar - u can generate ur own task cache keys
h
Ah — So, task cache key is just a string and in this case, it is set to “always” — is this correct?
d
I guess so. Setting it to always is bad 😄
Oh i was wrong about the previous comment.
h
Here’s what I found on
cache_key_fn
and why I agree with you:
An optional callable that, given the task run context and call parameters, generates a string key; if the key matches a previous completed state, that state result will be restored instead of running the task again.
This method is ok for most cases tbh
h
Based on what I have seen I have not came across a state named always — it is kind of funny though
d
its not a state
h
right. it is a string
d
Nope. If the cache key is always "always", when ever ur function is called , it will always return the result returned by the function exec when it was first called.
h
even if I call the function subsequently, it will continue to return the result when it was first called?
d
yes
as far as i know
h
sorry if you already covered this — is there an advantage of using a lambda function over just a string?
d
Cached tasks when completed registers the cache key to the server along with the storage path. Since ur task always has the same cache key "always" , it will always be a cache hit.
1
👍 1
I could be wrong. where did u get the lamda example from?
h
I am still searching… It is in the Prefect documentation
d
Ur answer to the lamda over string is here
An optional callable that, given the task run context and call parameters, generates a string key; if the key matches a previous completed state, that state result will be restored instead of running the task again.
it takes in a callable [meaning a function or any object with
__call__
method implemented ]
a string isnt a callable
h
thank you @Deceivious 👍