全部產品
Search
文件中心

DataWorks:Lindorm Ray節點

更新時間:Apr 04, 2026

DataWorks的Lindorm Ray節點支援使用Ray分散式運算架構進行Python任務的開發和周期性調度。本文為您介紹使用Lindorm Ray節點進行任務開發的主要流程。

節點介紹

Lindorm計算引擎提供基於Ray分散式運算架構的計算服務,相容開源Ray介面,支援Python編程模型,適用於分散式運算、機器學習、資料處理等情境。通過DataWorks的Lindorm Ray節點,線上編寫Python代碼並配置Ray提交命令,實現Ray作業的開發、調試和周期性調度。

使用限制

  • 資源群組限制:Lindorm Ray節點僅支援通過Serverless資源群組運行。

  • 語言限制:Lindorm Ray節點僅支援Python語言。

  • 運行限制:不支援單行或代碼塊運行,僅支援整體提交運行。

準備工作

  • 已建立Lindorm執行個體並綁定至DataWorks工作空間,詳情請參見綁定Lindorm計算資源

  • 已在Lindorm控制台建立Ray資源群組,詳情請參見使用RAY資源群組

  • (可選,RAM帳號需要)進行任務開發的RAM帳號已被添加至對應工作空間中,並具有開發空間管理員(許可權較大,謹慎添加)角色許可權,新增成員的操作詳情請參見為工作空間增加空間成員

    說明

    如果您使用的是主帳號,則忽略該添加操作。

建立Lindorm Ray節點

建立入口參考:建立Lindorm Ray節點

開發Lindorm Ray節點

Lindorm Ray節點的開發包含兩部分:在代碼編輯區編寫Python代碼,並在Ray提交命令區配置作業提交命令。

樣本一:編寫Python代碼直接運行

以使用蒙特卡洛方法估算圓周率為例,為您介紹如何配置和使用Lindorm Ray節點。

步驟一:編寫Python代碼

在Lindorm Ray節點的代碼編輯區中,編寫以下Python代碼。該樣本使用Ray的@ray.remote裝飾器將計算任務分發到多個Worker並存執行,通過隨機採樣估算圓周率。

import ray
import random
import time
import sys

ray.init()

@ray.remote
def compute_points_in_circle(num_points: int) -> int:
    """
    遠程函數:在單位正方形內隨機產生點,統計落在單位圓內的數量
    """
    inside = 0
    for _ in range(num_points):
        x = random.uniform(0, 1)
        y = random.uniform(0, 1)
        if x * x + y * y <= 1:
            inside += 1
    return inside

def estimate_pi(total_points: int, num_workers: int = 4) -> float:
    """
    使用 Ray 並行估算 π
    """
    start_time = time.time()
    points_per_worker = total_points // num_workers

    print(f"開始估算 π,總點數: {total_points:,},工作節點: {num_workers}")

    # 提交多個遠程任務
    tasks = [compute_points_in_circle.remote(points_per_worker) for _ in range(num_workers)]

    # 收集結果
    results = ray.get(tasks)
    total_inside = sum(results)

    # 計算 π
    pi_estimate = 4.0 * total_inside / total_points

    duration = time.time() - start_time

    print("=" * 60)
    print("Ray Sample: Calculating π using Monte Carlo Method")
    print("=" * 60)
    print(f"Total samples: {total_points:,}")
    print(f"Points inside circle: {total_inside:,}")
    print(f"Estimated π: {pi_estimate:.10f}")
    print(f"Time taken: {duration:.4f} seconds")
    print(f"Workers used: {num_workers}")
    print("=" * 60)
    print("Calculation complete!")

    return pi_estimate

if __name__ == "__main__":
    estimated_pi = estimate_pi(total_points=int(sys.argv[1]), num_workers=4)
    ray.shutdown()

步驟二:配置Ray提交命令

在節點的Ray提交命令地區中,配置以下提交命令。

ray job submit \ 
--working-dir "." \
-- python calculate_pi.py 1000000
重要
  • --working-dir參數值為本地路徑時,-- python參數所指定的檔案名稱需要與節點同名或與python代碼引用的資源檔同名。

  • --working-dir 為本地路徑,作業提交時--address--submission-id--working-dir參數的值由平台自動控制。如果您在提交命令中手動設定了上述參數,其值將被平台覆蓋。若--working-dir使用者填寫的值是https://開頭的遠程路徑,那麼不會覆蓋(例如直接使用oss路徑)。

節點內容配置說明

您可參照以下參數配置資訊,配置Lindorm Ray節點內容。

配置地區

參數名稱

參數描述

代碼編輯區

Python代碼

編寫使用Ray架構的Python代碼。支援ray.init()初始化、@ray.remote裝飾器等Ray API。

Ray提交命令

提交命令

配置Ray作業的提交命令。命令格式為ray job submit [options] -- python script.py [args]

runtime-env-json

可選。配置運行時環境。例如通過pip欄位安裝額外的Python依賴包,樣本:--runtime-env-json '{"pip": ["numpy", "pandas"]}'

參數

填寫您所需傳入代碼的參數資訊。您可將該參數配置為動態參數${var}

樣本二:引用DataWorks資源檔運行

當您的Ray作業包含多個Python模組檔案時,可以將公用模組上傳為DataWorks資源檔,並在主程式中通過##@resource_reference文法引用。

步驟一:建立並上傳資源檔

在DataWorks中建立以下兩個Python資源檔。

資源檔一:circle_utils.py

"""
計算圓內點的工具模組
"""
import random

def compute_points_in_circle(num_points: int) -> int:
    """
    在單位正方形內隨機產生點,統計落在單位圓內的數量
    """
    inside = 0
    for _ in range(num_points):
        x = random.uniform(0, 1)
        y = random.uniform(0, 1)
        if x * x + y * y <= 1:
            inside += 1
    return inside

資源檔二:pi_estimator.py

"""
π 估算工具模組
"""
import time

def estimate_pi(results, total_points: int) -> float:
    """
    根據圓內點數估算 π
    """
    start_time = time.time()
    total_inside = sum(results)
    pi_estimate = 4.0 * total_inside / total_points
    duration = time.time() - start_time

    print(f"總點數: {total_points:,}")
    print(f"圓內點數: {total_inside:,}")
    print(f"估算 π ≈ {pi_estimate:.6f}")
    print(f"耗時: {duration:.2f} 秒")

    return pi_estimate

步驟二:編寫主程式碼

在Lindorm Ray節點的代碼編輯區中,通過##@resource_reference引用已上傳的資源檔,並編寫主程式碼。

##@resource_reference{"circle_utils.py"}
##@resource_reference{"pi_estimator.py"}
"""
使用 Ray 並行計算 π 主程式
"""
import sys
import ray
from circle_utils import compute_points_in_circle
from pi_estimator import estimate_pi

@ray.remote
def compute_points_in_circle_remote(num_points: int) -> int:
    """
    遠程函數:封裝計算函數
    """
    return compute_points_in_circle(num_points)

if __name__ == "__main__":
    ray.init()

    total_points = int(sys.argv[1])
    num_workers = 4
    points_per_worker = total_points // num_workers

    print(f"開始估算 π,總點數: {total_points:,},工作節點: {num_workers}")

    # 提交多個遠程任務
    tasks = [compute_points_in_circle_remote.remote(points_per_worker) for _ in range(num_workers)]

    # 擷取結果
    results = ray.get(tasks)

    # 計算 π
    estimated_pi = estimate_pi(results, total_points)

    # 關閉 Ray
    ray.shutdown()

步驟三:配置Ray提交命令

在節點的Ray提交命令地區中,配置以下提交命令。

ray job submit \
--working-dir "." \
-- python main.py 1000000
說明

引用DataWorks資源檔時,平台會自動將資源檔下載到工作目錄中,通過--working-dir "."參數指定當前工作目錄即可在代碼中正常匯入引用的模組。

調試Lindorm Ray節點

  1. 配置調試屬性。

    您可在節點右側回合組態中配置計算資源Lindorm資源群組資源群組資訊,具體參數資訊如下。

    參數名稱

    描述

    計算資源

    選擇您所綁定的Lindorm計算資源。

    Lindorm資源群組

    選擇您在Lindorm控制台建立的Ray資源群組。

    資源群組

    選擇已通過網路連通性測試的Serverless資源群組。Lindorm Ray節點僅支援Serverless資源群組。

    指令碼參數

    在配置節點內容時,通過${參數名}的方式定義變數,需要在指令碼參數處配置參數名參數值資訊,任務運行時會將它動態替換為真實的取值。詳情請參見調度參數來源及其運算式

  2. 調試運行節點。

    執行節點任務,您需單擊儲存運行節點任務。

後續步驟

  • 節點調度配置:若專案目錄下的節點需要周期性調度執行,您需要在節點右側的調度配置中設定調度策略,配置相關的調度屬性。

  • 節點發布:若任務需要發布至生產環境執行,請單擊介面image表徵圖喚起發布流程,通過該流程將任務發布至生產環境。專案目錄下的節點只有在發布至生產環境後,才會進行周期性調度。