本次實(shí)驗(yàn)采用 2 臺(tái) Mac,系統(tǒng) macOS Mojave Version 10.14.3。
brew install python
pip3 install virtualenv
cd ~/Tools
virtualenv -p /usr/local/bin/python3 git_ray_env
source ./git_ray/env/bin/active
然后,查看虛擬環(huán)境 Python 的版本
$ python -V
Python 3.7.3
注意:確保兩臺(tái)機(jī)器 Python 版本一致。
pip install ray
Ray 的版本是 0.7.0
兩臺(tái)機(jī)器IP分別為:192.168.1.6
和 192.168.1.9
192.168.1.6
作為 Head,另一臺(tái)作為 Node。
在 Head 機(jī)器上執(zhí)行:
ray start --head --redis-port=6379
啟動(dòng)輸入如下:
2019-06-23 15:04:18,601INFO scripts.py:289 -- Using IP address 192.168.1.6 for this node.
2019-06-23 15:04:18,602INFO 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,710INFO services.py:409 -- Waiting for redis server at 127.0.0.1:6379 to respond...
2019-06-23 15:04:18,835INFO services.py:409 -- Waiting for redis server at 127.0.0.1:25445 to respond...
2019-06-23 15:04:18,840INFO services.py:806 -- Starting Redis shard with 3.44 GB max memory.
2019-06-23 15:04:18,855INFO 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,856INFO services.py:1441 -- Starting the Plasma object store with 5.15 GB memory using /tmp.
2019-06-23 15:04:18,877INFO 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 機(jī)器上執(zhí)行:
ray start --redis-address=192.168.1.6:6379
啟動(dòng)輸入如下:
2019-06-23 15:08:37,474INFO services.py:409 -- Waiting for redis server at 192.168.1.6:6379 to respond...
2019-06-23 15:08:37,503INFO scripts.py:363 -- Using IP address 192.168.1.9 for this node.
2019-06-23 15:08:37,551INFO 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,551INFO services.py:1441 -- Starting the Plasma object store with 5.15 GB memory using /tmp.
2019-06-23 15:08:37,566INFO 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)
運(yùn)行:python hello_world.py
輸出:
Hello world!
聯(lián)系客服