Passing os.environ in ray-decorated functions

Hello,

Here is an example code, where in the main part I set up an os.environ which I need in my function, decorated with @ray.remote:

import os
import ray

ray.init()

@ray.remote
def f(x):
    print (os.environ['MYTEST'])
    return x * x

# let's set an environ variable
os.environ['MYTEST'] = 'testing'
futures = [f.remote(i) for i in range(4)]
print(ray.get(futures)) # [0, 1, 4, 9]

If I call f(i) without the decorations, I have access to os.environ[‘MYTEST’], with @ray.remote I don’t.

Is there an elegant way to access “globals” from the main part of the code within ray-decorated functions? I can probably pass os.environ as an argument to f(x) but it’s not that elegant.

If you need access to a “global” config, one way is to use a named actor, e.g., “a = Actor.options(name=“global”).remote()”. Then, any task can retrieve a handle to that actor a with ray.get_actor("global") and query the config.

The other option is to pass the config explicitly as an arg as you mentioned.

1 Like