日常记录
Pandas 基本操作
numpy基本函数
numpy线性代数
ray 的使用
chisel
正向代理与反向代理
HTTPS 协议
Jupyter 配置
Python vscode配置
本文档使用 MrDoc 发布
-
+
首页
ray 的使用
### 基础功能 代码示例: ``` import ray ray.init(ignore_reinit_error=True) @ray.remote def add(a, b): return a + b @ray.remote(num_cpus=2, num_gpus=0.5) class Counter(object): def __init__(self): self.value = 0 def incr(self): self.value += 1 return self.value def decr(self): self.value -= 1 return self.value task_id = add.remote(1, 1) result = ray.get(task_id) # 创建多个actor counter01 = Counter.remote() counter02 = Counter.remote() ``` ### 并行与串行 - 使用`remote`修饰函数,则函数是并行的,比如`add.remote`是并行执行的; - 在同一个`actor`上运行的方法顺序执行,并且共享状态; - 在不同`actor`上运行的方法并行执行; 下面通过例子说明: ```python # 这个是并行执行的 for _ in range(4): add.remote(1, 1) # 创建10个actor counters = [Counter.remote() for _ in range(10)] # 同时运行所有的counter 得到results,这些tasks会并行执行 results = ray.get([c.increment.remote() for c in counters]) print(results) # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] # 运行第一个counter五次 并得到 results,这些tasks会顺序执行并共享状态 results = ray.get([counters[0].increment.remote() for _ in range(5)]) print(results) # [2, 3, 4, 5, 6] ``` ### 命名的acotr > actor可以被命名,这使得你可以从集群的任意一个节点获取到其他节点创建的actor 看一个简单的示例: ``` # Create an actor with a name counter = Counter.options(name="myactor").remote() # Retrieve the actor later somewhere counter = ray.get_actor("myactor") ``` 需要注意的事情: - 命名的actor可以归属到命名空间`namespace`,如果创建时没有指定namespace,则使用默认的`anonymous`命名空间; - 创建命名的actor时可以指定参数`get_if_exists=True`,避免重复创建; - 创建命名的actor时可以指定参数`lifetime="detached"`,这样即使创建它的进程退出,actor还可以继续存在,可以被其他进程获取; 更复杂的示例: ``` import ray @ray.remote class Actor: pass # driver_1.py # Job 1 creates an actor, "orange" in the "colors" namespace. ray.init(address="auto", namespace="colors") Actor.options(name="orange", lifetime="detached", get_if_exists=True).remote() # driver_2.py # Job 2 is now connecting to a different namespace. ray.init(address="auto", namespace="fruit") # This fails because "orange" was defined in the "colors" namespace. ray.get_actor("orange") # You can also specify the namespace explicitly. ray.get_actor("orange", namespace="colors") # driver_3.py # Job 3 connects to the original "colors" namespace ray.init(address="auto", namespace="colors") # This returns the "orange" actor we created in the first job. ray.get_actor("orange") ``` ### 终止actor > 如果actor的所有引用对象在python运行时中都已经被删除,或者当创建actor的进程退出时,该actor也会被停止并删除 你也可以主动通知actor退出,函主要有两种方式: - 在actor进程中,如果想停止执行,可以这样: ``` ray.actor.exit_actor() ``` - 在主进程中,如果想杀掉某个actor进程,可以这样: ``` ray.kill(actor_handle) ``` ### ActorPool 这个类和`multiprocessing.Pool`很类似,允许用户在一个固定的`actor pool`中运行tasks ``` import ray from ray.util import ActorPool @ray.remote class Actor: def double(self, n): return n * 2 a1, a2 = Actor.remote(), Actor.remote() pool = ActorPool([a1, a2]) # pool.map(..) returns a Python generator object ActorPool.map gen = pool.map(lambda a, v: a.double.remote(v), [1, 2, 3, 4]) print(list(gen)) # [2, 4, 6, 8] ``` ### 参考 > [Ray官方文档:Named Actors](https://docs.ray.io/en/latest/ray-core/actors/named-actors.html)
gaojian
2022年12月8日 15:06
分享文档
收藏文档
上一篇
下一篇
微信扫一扫
复制链接
手机扫一扫进行分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码