My goal is to make the code below execute in roughly 0.3 instead of 0.5 seconds. I've tried using the decorators from functools.lru_cache, toolz.functoolz.memoize and kids.cache.cache on foo but none of those has worked (either error message or didn't correctly execute). What can I do to make this work?
import ray
@ray.remote
class Foo:
def foo(self, x):
print("executing foo with {}".format(x))
time.sleep(0.1)
ray.init()
f = Foo.remote()
s = time.time()
ray.get([f.foo.remote(x=i) for i in [1, 2, 1, 4, 1]])
print(time.time()-s)
ray.shutdown()
General warning: Caching arbitrary function calls can be dangerous if the function produces side effects.
In this case, presumably you want the program to output
Those other cache tools you mentioned don't tend to work well with Ray because they try to store the cache in global state of some sort, and they aren't storing that state in a place that can be accessed in a distributed way. Since you already have an actor, you could just store your global state in the actor.
This is a pretty generic caching technique, the only important difference here was that we had to store our state in the actor.
More generalized approach
We can also generalize this approach for any Ray function by defining a general purpose function cache:
Then to clean up the way we use it we can define a decorator:
Finally, to use this cache: