I have three files as follows
""" main.py """
import time
import ray
class LocalBuffer(dict):
def __call__(self):
return self
@ray.remote
class Worker():
def __init__(self, learner):
self.local = LocalBuffer()
self.learner = learner
def sample(self):
for i in range(10):
self.local.update({
'state': [1, 2, 3]
})
print(self.local)
self.learner.update_buffer.remote(self.local)
@ray.remote
class Learner():
def __init__(self):
self.buffer = {}
def update_buffer(self, local_buffer):
print(local_buffer)
self.buffer['state'] = local_buffer['state']
ray.init()
learner = Learner.remote()
worker = Worker.remote(learner)
worker.sample.remote()
time.sleep(10)
The above code will work fine if I remove all code related to ray
. If not, something wrong happens. The error message says there is no state
in local_buffer
in update_buffer
. I know that the error arises because of LocalBuffer
defined in worker.py
-- If I define Worker.local
as a built-in dict
, everything will be fine. But why can't I use LocalBuffer
? I do need it here and I have no idea how to make it work.
Update
I see where the problem is. The reason is that worker
and learner
are in different processes. And a user-defined object such as self.local
cannot be passed between processes. For this specific problem, I can get rid of the problem by casting self.local
to dict
when self.local
is passed to self.learner.update_buffer
. I've tried to import LocalBuffer
in learner.py
, but it did not work. Maybe I have to learn more about multiprocessing to figure it out. I'll be very grateful if anyone is willing to fill me in some useful information.