import ray
from ACppLibrary import NonSerializable
class Solve1:
def __init__():
self.obj = NonSerializable()
self.obj.slow_read()
def run():
self.obj.run()
class Solve2:
obj = NonSerializable()
obj.slow_read()
def run():
obj.run()
if __name__ == '__main__':
ray.init(num_cpus=8)
# Each process creates a new non-serializable, and reads data separately
l1 = [Solve1.remote() for _ in range(5)]
ref1 = [s.run.remote() for s in l1]
ray.get(ref1)
# Doesn't run. "TypeError: Could not serialize the actor class"
l2 = [Solve2.remote() for _ in range(5)]
ref2 = [s.run.remote() for s in l2]
ray.get(ref2)
请问一下,如何在ray的进程之间共享不可序列化的内存
如上面的代码所示,我需要建一个不可序列化的对象NonSerializable。在第一个类Solve1里面,这个对象被建成对象属性,导致结果是每个进程都会建立一个独立的对象,并且读取数据,消耗大量的内存;在第二个类Solve2里面,我把对象属性改成了类属性,但是无法运行,报错无法序列化。
谢谢