https://prefect.io logo
h

healtheworld

07/14/2023, 9:11 AM
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

Deceivious

07/14/2023, 12:46 PM
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

healtheworld

07/14/2023, 2:57 PM
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

Deceivious

07/14/2023, 3:08 PM
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

healtheworld

07/14/2023, 3:38 PM
Ah — So, task cache key is just a string and in this case, it is set to “always” — is this correct?
d

Deceivious

07/14/2023, 3:39 PM
I guess so. Setting it to always is bad 😄
Oh i was wrong about the previous comment.
h

healtheworld

07/14/2023, 3:40 PM
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

healtheworld

07/14/2023, 3:41 PM
Based on what I have seen I have not came across a state named always — it is kind of funny though
d

Deceivious

07/14/2023, 3:42 PM
its not a state
h

healtheworld

07/14/2023, 3:42 PM
right. it is a string
d

Deceivious

07/14/2023, 3:43 PM
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

healtheworld

07/14/2023, 3:45 PM
even if I call the function subsequently, it will continue to return the result when it was first called?
d

Deceivious

07/14/2023, 3:45 PM
yes
as far as i know
h

healtheworld

07/14/2023, 3:46 PM
sorry if you already covered this — is there an advantage of using a lambda function over just a string?
d

Deceivious

07/14/2023, 3:46 PM
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

healtheworld

07/14/2023, 3:49 PM
I am still searching… It is in the Prefect documentation
d

Deceivious

07/14/2023, 3:50 PM
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
image.png
h

healtheworld

07/14/2023, 3:56 PM
thank you @Deceivious 👍