欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ray 环境搭建和示例

程序员文章站 2022-07-12 17:13:51
...

Ray 环境搭建和示例

Ray 环境设置

本次实验采用 2 台 Mac,系统 macOS Mojave Version 10.14.3。

安装 Python 3

brew install python

安装 Python 虚拟环境

pip3 install virtualenv
cd ~/Tools
virtualenv -p /usr/local/bin/python3 git_ray_env
source ./git_ray/env/bin/active	

然后,查看虚拟环境 Python 的版本

$ python -V
Python 3.7.3

注意:确保两台机器 Python 版本一致。

安装 Ray

pip install ray

Ray 的版本是 0.7.0

以 Cluster 模式运行 Ray

两台机器IP分别为:192.168.1.6192.168.1.9

192.168.1.6 作为 Head,另一台作为 Node。

在 Head 机器上执行:

ray start --head --redis-port=6379

启动输入如下:

2019-06-23 15:04:18,601	INFO scripts.py:289 -- Using IP address 192.168.1.6 for this node.
2019-06-23 15:04:18,602	INFO node.py:497 -- Process STDOUT and STDERR is being redirected to /tmp/ray/session_2019-06-23_15-04-18_601521_88785/logs.
2019-06-23 15:04:18,710	INFO services.py:409 -- Waiting for redis server at 127.0.0.1:6379 to respond...
2019-06-23 15:04:18,835	INFO services.py:409 -- Waiting for redis server at 127.0.0.1:25445 to respond...
2019-06-23 15:04:18,840	INFO services.py:806 -- Starting Redis shard with 3.44 GB max memory.
2019-06-23 15:04:18,855	INFO node.py:511 -- Process STDOUT and STDERR is being redirected to /tmp/ray/session_2019-06-23_15-04-18_601521_88785/logs.
2019-06-23 15:04:18,856	INFO services.py:1441 -- Starting the Plasma object store with 5.15 GB memory using /tmp.
2019-06-23 15:04:18,877	INFO scripts.py:319 --
Started Ray on this node. You can add additional nodes to the cluster by calling

    ray start --redis-address 192.168.1.6:6379

from the node you wish to add. You can connect a driver to the cluster from Python by running

    import ray
    ray.init(redis_address="192.168.1.6:6379")

If you have trouble connecting from a different machine, check that your firewall is configured properly. If you wish to terminate the processes that have been started, run

    ray stop

在 Node 机器上执行:

ray start --redis-address=192.168.1.6:6379

启动输入如下:

2019-06-23 15:08:37,474	INFO services.py:409 -- Waiting for redis server at 192.168.1.6:6379 to respond...
2019-06-23 15:08:37,503	INFO scripts.py:363 -- Using IP address 192.168.1.9 for this node.
2019-06-23 15:08:37,551	INFO node.py:511 -- Process STDOUT and STDERR is being redirected to /tmp/ray/session_2019-06-23_15-04-18_601521_88785/logs.
2019-06-23 15:08:37,551	INFO services.py:1441 -- Starting the Plasma object store with 5.15 GB memory using /tmp.
2019-06-23 15:08:37,566	INFO scripts.py:371 --
Started Ray on this node. If you wish to terminate the processes that have been started, run

    ray stop

示例

hello_world.py

import ray

ray.init(redis_address="192.168.1.6:6379")

@ray.remote
def hello():
    return "Hello"

@ray.remote
def world():
    return "world!"

@ray.remote
def hello_world(a, b):
    return a + " " + b

a_id = hello.remote()
b_id = world.remote()
c_id = hello_world.remote(a_id, b_id)

hello = ray.get(c_id)

print(hello)

运行:python hello_world.py

输出:

Hello world!

参考

  1. [https://blog.csdn.net/Uwr44UOuQcNsUQb60zk2/article/details/79029868)
相关标签: 分布式计算