最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中的分布式框架Ray的安裝與使用教程

 更新時(shí)間:2024年08月28日 12:24:45   作者:DECHIN  
Ray框架下不僅可以通過conda和Python十分方便的構(gòu)建一個(gè)集群,還可以自動(dòng)的對(duì)分布式任務(wù)進(jìn)行并發(fā)處理,且支持GPU分布式任務(wù)的提交,本文介紹了基于Python的分布式框架Ray的基本安裝與使用,感興趣的朋友一起看看吧

技術(shù)背景

假設(shè)我們?cè)谝粋€(gè)局域網(wǎng)內(nèi)有多臺(tái)工作站(不是服務(wù)器),那么有沒有一個(gè)簡單的方案可以實(shí)現(xiàn)一個(gè)小集群,提交分布式的任務(wù)呢?Ray為我們提供了一個(gè)很好的解決方案,允許你通過conda和Python靈活的構(gòu)建集群環(huán)境,并提交分布式的任務(wù)。其基本架構(gòu)為:

那么本文簡單的介紹一下Ray的安裝與基本使用。

安裝

由于是一個(gè)Python的框架,Ray可以直接使用pip進(jìn)行安裝和管理:

$ python3 -m pip install ray[default]

但是需要注意的是,在所有需要構(gòu)建集群的設(shè)備上,需要統(tǒng)一Python和Ray的版本,因此建議先使用conda創(chuàng)建同樣的虛擬環(huán)境之后,再安裝統(tǒng)一版本的ray。否則在添加集群節(jié)點(diǎn)的時(shí)候就有可能出現(xiàn)如下問題:

RuntimeError: Version mismatch: The cluster was started with:
    Ray: 2.7.2
    Python: 3.7.13
This process on node 172.17.0.2 was started with:
    Ray: 2.7.2
    Python: 3.7.5

啟動(dòng)和連接服務(wù)

一般在配置集群的時(shí)候可以先配置下密鑰登陸:

$ ssh-keygen -t rsa
$ ssh-copy-id user_name@ip_address

就這么兩步,就可以配置遠(yuǎn)程服務(wù)器ssh免密登陸(配置的過程中有可能需要輸入一次密碼)。然后在主節(jié)點(diǎn)(配置一個(gè)master節(jié)點(diǎn))啟動(dòng)ray服務(wù):

$ ray start --head --dashboard-host='0.0.0.0' --dashboard-port=8265
Usage stats collection is enabled. To disable this, add `--disable-usage-stats` to the command that starts the cluster, or run the following command: `ray disable-usage-stats` before starting the cluster. See https://docs.ray.io/en/master/cluster/usage-stats.html for more details.
Local node IP: xxx.xxx.xxx.xxx
--------------------
Ray runtime started.
--------------------
Next steps
  To add another node to this Ray cluster, run
    ray start --address='xxx.xxx.xxx.xxx:6379'
  To connect to this Ray cluster:
    import ray
    ray.init()
  To submit a Ray job using the Ray Jobs CLI:
    RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . -- python my_script.py
  See https://docs.ray.io/en/latest/cluster/running-applications/job-submission/index.html
  for more information on submitting Ray jobs to the Ray cluster.
  To terminate the Ray runtime, run
    ray stop
  To view the status of the cluster, use
    ray status
  To monitor and debug Ray, view the dashboard at
    xxx.xxx.xxx.xxx:8265
  If connection to the dashboard fails, check your firewall settings and network configuration.

這就啟動(dòng)完成了,并給你指示了下一步的操作,例如在另一個(gè)節(jié)點(diǎn)上配置添加到集群中,可以使用指令:

$ ray start --address='xxx.xxx.xxx.xxx:6379'

但是前面提到了,這里要求Python和Ray版本要一致,如果版本不一致就會(huì)出現(xiàn)這樣的報(bào)錯(cuò):

RuntimeError: Version mismatch: The cluster was started with:
    Ray: 2.7.2
    Python: 3.7.13
This process on node 172.17.0.2 was started with:
    Ray: 2.7.2
    Python: 3.7.5

到這里其實(shí)Ray集群就已經(jīng)部署完成了,非常的簡單方便。

基礎(chǔ)使用

我們先用一個(gè)最簡單的案例來測(cè)試一下:

# test_ray.py 
import os
import ray
ray.init()
print('''This cluster consists of
    {} nodes in total
    {} CPU resources in total
'''.format(len(ray.nodes()), ray.cluster_resources()['CPU']))

這個(gè)Python腳本打印了遠(yuǎn)程節(jié)點(diǎn)的計(jì)算資源,那么我們可以用這樣的方式去提交一個(gè)本地的job:

$ RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . -- python test_ray.py 
Job submission server address: http://xxx.xxx.xxx.xxx:8265
2024-08-27 07:35:10,751 INFO dashboard_sdk.py:338 -- Uploading package gcs://_ray_pkg_4b79155b5de665ce.zip.
2024-08-27 07:35:10,751 INFO packaging.py:518 -- Creating a file package for local directory '.'.
-------------------------------------------------------
Job 'raysubmit_7Uqy8LjP4dxjZxGa' submitted successfully
-------------------------------------------------------
Next steps
  Query the logs of the job:
    ray job logs raysubmit_7Uqy8LjP4dxjZxGa
  Query the status of the job:
    ray job status raysubmit_7Uqy8LjP4dxjZxGa
  Request the job to be stopped:
    ray job stop raysubmit_7Uqy8LjP4dxjZxGa
Tailing logs until the job exits (disable with --no-wait):
2024-08-27 15:35:14,079 INFO worker.py:1330 -- Using address xxx.xxx.xxx.xxx:6379 set in the environment variable RAY_ADDRESS
2024-08-27 15:35:14,079 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: xxx.xxx.xxx.xxx:6379...
2024-08-27 15:35:14,103 INFO worker.py:1639 -- Connected to Ray cluster. View the dashboard at http://xxx.xxx.xxx.xxx:8265 
This cluster consists of
    1 nodes in total
    48.0 CPU resources in total
------------------------------------------
Job 'raysubmit_7Uqy8LjP4dxjZxGa' succeeded
------------------------------------------

這里的信息說明,遠(yuǎn)程的集群只有一個(gè)節(jié)點(diǎn),該節(jié)點(diǎn)上有48個(gè)可用的CPU核資源。這些輸出信息不僅可以在終端窗口上看到,還可以從這里給出的dashboard鏈接里面看到更加詳細(xì)的任務(wù)管理情況:

這里也順便提交一個(gè)輸出軟件位置信息的指令,確認(rèn)下任務(wù)是在遠(yuǎn)程執(zhí)行而不是在本地執(zhí)行:

import ray
ray.init()
import numpy as np
print (np.__file__)

返回的日志為:

$ RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . -- python test_ray.py 
Job submission server address: http://xxx.xxx.xxx.xxx:8265
2024-08-27 07:46:10,645 INFO dashboard_sdk.py:338 -- Uploading package gcs://_ray_pkg_5bba1a7144beb522.zip.
2024-08-27 07:46:10,658 INFO packaging.py:518 -- Creating a file package for local directory '.'.
-------------------------------------------------------
Job 'raysubmit_kQ3XgE4Hxp3dkmuU' submitted successfully
-------------------------------------------------------
Next steps
  Query the logs of the job:
    ray job logs raysubmit_kQ3XgE4Hxp3dkmuU
  Query the status of the job:
    ray job status raysubmit_kQ3XgE4Hxp3dkmuU
  Request the job to be stopped:
    ray job stop raysubmit_kQ3XgE4Hxp3dkmuU
Tailing logs until the job exits (disable with --no-wait):
2024-08-27 15:46:12,456 INFO worker.py:1330 -- Using address xxx.xxx.xxx.xxx:6379 set in the environment variable RAY_ADDRESS
2024-08-27 15:46:12,457 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: xxx.xxx.xxx.xxx:6379...
2024-08-27 15:46:12,470 INFO worker.py:1639 -- Connected to Ray cluster. View the dashboard at http://xxx.xxx.xxx.xxx:8265 
/home/dechin/anaconda3/envs/mindspore-latest/lib/python3.7/site-packages/numpy/__init__.py
------------------------------------------
Job 'raysubmit_kQ3XgE4Hxp3dkmuU' succeeded
------------------------------------------
$ python3 -m pip show numpy
Name: numpy
Version: 1.21.6
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: 
License: BSD
Location: /usr/local/python-3.7.5/lib/python3.7/site-packages
Requires: 
Required-by: CyFES, h5py, hadder, matplotlib, mindinsight, mindspore, mindspore-serving, pandas, ray, scikit-learn, scipy

這里可以看到,提交的任務(wù)中numpy是保存在mindspore-latest虛擬環(huán)境中的,而本地的numpy不在虛擬環(huán)境中,說明任務(wù)確實(shí)是在遠(yuǎn)程執(zhí)行的。類似的可以在dashboard上面看到提交日志:

接下來測(cè)試一下分布式框架ray的并發(fā)特性:

import ray
ray.init()
@ray.remote(num_returns=1)
def cpu_task():
    import time
    time.sleep(2)
    import numpy as np
    nums = 100000
    arr = np.random.random((2, nums))
    arr2 = arr[1]**2 + arr[0]**2
    pi = np.where(arr2<=1, 1, 0).sum() * 4 / nums
    return pi
num_conc = 10
res = ray.get([cpu_task.remote() for _ in range(num_conc)])
print (sum(res) / num_conc)

這個(gè)案例的內(nèi)容是用蒙特卡洛算法計(jì)算圓周率的值,一次提交10個(gè)任務(wù),每個(gè)任務(wù)中撒點(diǎn)100000個(gè),并休眠2s。那么如果是順序執(zhí)行的話,理論上需要休眠20s。而這里提交任務(wù)之后,輸出如下:

$ time RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . --entrypoint-num-cpus 10 -- python te
st_ray.py 
Job submission server address: http://xxx.xxx.xxx.xxx:8265
2024-08-27 08:30:13,315 INFO dashboard_sdk.py:385 -- Package gcs://_ray_pkg_d66b052eb6944465.zip already exists, skipping upload.
-------------------------------------------------------
Job 'raysubmit_Ur6MAvP7DYiCT6Uz' submitted successfully
-------------------------------------------------------
Next steps
  Query the logs of the job:
    ray job logs raysubmit_Ur6MAvP7DYiCT6Uz
  Query the status of the job:
    ray job status raysubmit_Ur6MAvP7DYiCT6Uz
  Request the job to be stopped:
    ray job stop raysubmit_Ur6MAvP7DYiCT6Uz
Tailing logs until the job exits (disable with --no-wait):
2024-08-27 16:30:15,032 INFO worker.py:1330 -- Using address xxx.xxx.xxx.xxx:6379 set in the environment variable RAY_ADDRESS
2024-08-27 16:30:15,033 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: xxx.xxx.xxx.xxx:6379...
2024-08-27 16:30:15,058 INFO worker.py:1639 -- Connected to Ray cluster. View the dashboard at http://xxx.xxx.xxx.xxx:8265 
3.141656
------------------------------------------
Job 'raysubmit_Ur6MAvP7DYiCT6Uz' succeeded
------------------------------------------
real    0m7.656s
user    0m0.414s
sys     0m0.010s

總的運(yùn)行時(shí)間在7.656秒,其中5s左右的時(shí)間是來自網(wǎng)絡(luò)delay。所以實(shí)際上并發(fā)之后的總運(yùn)行時(shí)間就在2s左右,跟單任務(wù)休眠的時(shí)間差不多。也就是說,遠(yuǎn)程提交的任務(wù)確實(shí)是并發(fā)執(zhí)行的。最終返回的結(jié)果進(jìn)行加和處理,得到的圓周率估計(jì)為:3.141656。而且除了普通的CPU任務(wù)之外,還可以上傳GPU任務(wù):

import ray
ray.init()
@ray.remote(num_returns=1, num_gpus=1)
def test_ms():
    import os
    os.environ['GLOG_v']='4'
    os.environ['CUDA_VISIBLE_DEVICE']='0'
    import mindspore as ms
    ms.set_context(device_target="GPU", device_id=0)
    a = ms.Tensor([1, 2, 3], ms.float32)
    return a.asnumpy().sum()
res = ray.get(test_ms.remote())
ray.shutdown()
print (res)

這個(gè)任務(wù)是用mindspore簡單創(chuàng)建了一個(gè)Tensor,并計(jì)算了Tensor的總和返回給本地,輸出內(nèi)容為:

$ RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . --entrypoint-num-gpus 1 -- python test_ray.py 
Job submission server address: http://xxx.xxx.xxx.xxx:8265
2024-08-28 01:16:38,712 INFO dashboard_sdk.py:338 -- Uploading package gcs://_ray_pkg_10019cd9fa9bdc38.zip.
2024-08-28 01:16:38,712 INFO packaging.py:518 -- Creating a file package for local directory '.'.

-------------------------------------------------------
Job 'raysubmit_RUvkEqnkjNitKmnJ' submitted successfully
-------------------------------------------------------

Next steps
  Query the logs of the job:
    ray job logs raysubmit_RUvkEqnkjNitKmnJ
  Query the status of the job:
    ray job status raysubmit_RUvkEqnkjNitKmnJ
  Request the job to be stopped:
    ray job stop raysubmit_RUvkEqnkjNitKmnJ

Tailing logs until the job exits (disable with --no-wait):
2024-08-28 09:16:41,960 INFO worker.py:1330 -- Using address xxx.xxx.xxx.xxx:6379 set in the environment variable RAY_ADDRESS
2024-08-28 09:16:41,960 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: xxx.xxx.xxx.xxx:6379...
2024-08-28 09:16:41,974 INFO worker.py:1639 -- Connected to Ray cluster. View the dashboard at http://xxx.xxx.xxx.xxx:8265 
6.0

------------------------------------------
Job 'raysubmit_RUvkEqnkjNitKmnJ' succeeded
------------------------------------------

返回的計(jì)算結(jié)果是6.0,那么也是正確的。

查看和管理任務(wù)

前面的任務(wù)輸出信息中,都有相應(yīng)的job_id,我們可以根據(jù)這個(gè)job_id在主節(jié)點(diǎn)上面查看相關(guān)任務(wù)的執(zhí)行情況:

$ ray job status raysubmit_RUvkEqnkjNitKmnJ

可以查看該任務(wù)的輸出內(nèi)容:

$ ray job logs raysubmit_RUvkEqnkjNitKmnJ

還可以終止該任務(wù)的運(yùn)行:

$ ray job stop raysubmit_RUvkEqnkjNitKmnJ

總結(jié)概要

本文介紹了基于Python的分布式框架Ray的基本安裝與使用。Ray框架下不僅可以通過conda和Python十分方便的構(gòu)建一個(gè)集群,還可以自動(dòng)的對(duì)分布式任務(wù)進(jìn)行并發(fā)處理,且支持GPU分布式任務(wù)的提交,極大的簡化了手動(dòng)分布式開發(fā)的工作量。

版權(quán)聲明

本文首發(fā)鏈接為:https://www.cnblogs.com/dechinphy/p/ray.html

作者ID:DechinPhy

更多原著文章:https://www.cnblogs.com/dechinphy/

請(qǐng)博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

到此這篇關(guān)于Python中的分布式框架Ray的安裝與使用教程的文章就介紹到這了,更多相關(guān)Python 分布式框架Ray內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)高效迭代固定大小記錄的專業(yè)指南

    Python實(shí)現(xiàn)高效迭代固定大小記錄的專業(yè)指南

    在數(shù)據(jù)處理、文件解析和網(wǎng)絡(luò)編程中,我們經(jīng)常需要處理由固定長度記錄組成的數(shù)據(jù)塊,本文將詳細(xì)介紹一下Python如何實(shí)現(xiàn)高效迭代固定大小記錄,有需要的小伙伴可以了解下
    2025-09-09
  • 淺談python中的getattr函數(shù) hasattr函數(shù)

    淺談python中的getattr函數(shù) hasattr函數(shù)

    下面小編就為大家?guī)硪黄獪\談python中的getattr函數(shù) hasattr函數(shù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • 關(guān)于win10在tensorflow的安裝及在pycharm中運(yùn)行步驟詳解

    關(guān)于win10在tensorflow的安裝及在pycharm中運(yùn)行步驟詳解

    這篇文章主要介紹了關(guān)于win10在tensorflow的安裝及在pycharm中運(yùn)行的步驟詳解,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 詳解python--模擬輪盤抽獎(jiǎng)游戲

    詳解python--模擬輪盤抽獎(jiǎng)游戲

    這篇文章主要介紹了python模擬輪盤抽獎(jiǎng)游戲,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python識(shí)別html主要文本框過程解析

    Python識(shí)別html主要文本框過程解析

    這篇文章主要介紹了python識(shí)別html主要文本框過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 布同 統(tǒng)計(jì)英文單詞的個(gè)數(shù)的python代碼

    布同 統(tǒng)計(jì)英文單詞的個(gè)數(shù)的python代碼

    最近需要翻譯英文文章,所以需要統(tǒng)計(jì)單詞個(gè)數(shù)。索性寫了一段代碼在此,可以簡單的統(tǒng)計(jì)單詞的個(gè)數(shù)
    2011-03-03
  • 一文帶你解鎖Python文件匹配技巧

    一文帶你解鎖Python文件匹配技巧

    在日常的文件操作和數(shù)據(jù)處理中,文件匹配是一個(gè)非常常見的任務(wù),本文將詳細(xì)介紹如何使用?Python?實(shí)現(xiàn)文件匹配,有需要的小伙伴可以參考下
    2024-12-12
  • 科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)

    科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)

    這篇文章主要為大家介紹了科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Pytorch中view()函數(shù)的實(shí)現(xiàn)示例

    Pytorch中view()函數(shù)的實(shí)現(xiàn)示例

    view函數(shù)是一個(gè)極為重要的張量操作函數(shù),本文主要介紹了Pytorch中view()函數(shù)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-05-05
  • macos環(huán)境升級(jí)自己安裝的python3完整指南

    macos環(huán)境升級(jí)自己安裝的python3完整指南

    在MacOS系統(tǒng)上,保持Pip和Python版本的最新狀態(tài)對(duì)于順利進(jìn)行Python開發(fā)至關(guān)重要,下面這篇文章主要介紹了macos環(huán)境升級(jí)自己安裝python3的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2026-04-04

最新評(píng)論

焦作市| 綦江县| 衡东县| 邮箱| 嘉定区| 沂南县| 腾冲县| 大姚县| 南郑县| 青浦区| 高阳县| 疏附县| 山阳县| 湘乡市| 如皋市| 喀什市| 弥勒县| 化州市| 大同县| 晋宁县| 靖安县| 尚义县| 清镇市| 清流县| 库尔勒市| 六盘水市| 闽清县| 中西区| 阿巴嘎旗| 基隆市| 连州市| 平邑县| 景东| 永顺县| 临西县| 长寿区| 辛集市| 荣成市| 牙克石市| 项城市| 湘西|