{"id":1174436,"date":"2025-01-15T17:17:58","date_gmt":"2025-01-15T09:17:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174436.html"},"modified":"2025-01-15T17:18:01","modified_gmt":"2025-01-15T09:18:01","slug":"%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8python%e4%b8%ad%e7%9a%84raypy","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1174436.html","title":{"rendered":"\u5982\u4f55\u8c03\u7528python\u4e2d\u7684raypy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26075921\/63eedd03-c39a-47d4-94d8-a134192f7d9b.webp\" alt=\"\u5982\u4f55\u8c03\u7528python\u4e2d\u7684raypy\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8c03\u7528RayPy\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a<strong>\u5b89\u88c5Ray\u5e93\u3001\u521d\u59cb\u5316Ray\u3001\u5b9a\u4e49Ray\u8fdc\u7a0b\u51fd\u6570\u3001\u8c03\u7528Ray\u8fdc\u7a0b\u51fd\u6570\u3001\u83b7\u53d6\u7ed3\u679c<\/strong>\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684<strong>\u5b89\u88c5Ray\u5e93<\/strong>\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b89\u88c5Ray\u5e93\uff1a<\/strong><\/p>\n<p>\u8981\u4f7f\u7528RayPy\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Ray\u5e93\u3002Ray\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u5e76\u884c\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\u7684Python\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install ray<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165Ray\u5e93\u5e76\u5f00\u59cb\u4f7f\u7528\u5b83\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Ray\u5e93<\/h3>\n<\/p>\n<p><p>Ray\u5e93\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u4fddPython\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\uff1a<\/strong>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u73af\u5883\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5Python\u662f\u5426\u5df2\u7ecf\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5Python\uff0c\u53ef\u4ee5\u4ece<a href=\"https:\/\/www.python.org\/downloads\/\">Python\u5b98\u65b9\u7f51\u7ad9<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u5408\u9002\u7248\u672c\u7684Python\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883\uff08\u53ef\u9009\uff09\uff1a<\/strong>\u4e3a\u4e86\u907f\u514d\u5e93\u4e4b\u95f4\u7684\u51b2\u7a81\uff0c\u5efa\u8bae\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u5e76\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv ray_env<\/p>\n<p>source ray_env\/bin\/activate  # \u5728Linux\u6216macOS\u4e0a<\/p>\n<p>ray_env\\Scripts\\activate  # \u5728Windows\u4e0a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Ray\u5e93\uff1a<\/strong>\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5Ray\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install ray<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -c &quot;import ray; print(ray.__version__)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u4f9d\u8d56\u9879\uff1a<\/strong>Ray\u5e93\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u4f9d\u8d56\u9879\uff0c\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u9879\u90fd\u5df2\u7ecf\u5b89\u88c5\u3002\u5982\u679c\u9047\u5230\u4efb\u4f55\u95ee\u9898\uff0c\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\u6216\u793e\u533a\u652f\u6301\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u521d\u59cb\u5316Ray<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Ray\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u521d\u59cb\u5316Ray\u3002\u521d\u59cb\u5316Ray\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u53c2\u6570\uff0c\u5982\u5e76\u884c\u5ea6\u3001\u5185\u5b58\u9650\u5236\u7b49\u3002\u4ee5\u4e0b\u662f\u521d\u59cb\u5316Ray\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165Ray\u5e93\uff1a<\/strong>\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165Ray\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ray<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521d\u59cb\u5316Ray\uff1a<\/strong>\u4f7f\u7528<code>ray.init()<\/code>\u51fd\u6570\u521d\u59cb\u5316Ray\u3002\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u53c2\u6570\uff0c\u5982<code>num_cpus<\/code>\u3001<code>num_gpus<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ray.init(num_cpus=4, num_gpus=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>num_cpus<\/code>\u53c2\u6570\u6307\u5b9a\u53ef\u7528\u7684CPU\u6570\u91cf\uff0c<code>num_gpus<\/code>\u53c2\u6570\u6307\u5b9a\u53ef\u7528\u7684GPU\u6570\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5Ray\u96c6\u7fa4\u72b6\u6001\uff1a<\/strong>\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ray.cluster_resources()<\/code>\u51fd\u6570\u68c0\u67e5Ray\u96c6\u7fa4\u7684\u8d44\u6e90\u72b6\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">resources = ray.cluster_resources()<\/p>\n<p>print(resources)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u5b9a\u4e49Ray\u8fdc\u7a0b\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Ray\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\u662f\u5b9a\u4e49\u8fdc\u7a0b\u51fd\u6570\u3002\u8fdc\u7a0b\u51fd\u6570\u662f\u901a\u8fc7Ray\u8fd0\u884c\u7684\u5e76\u884c\u6216\u5206\u5e03\u5f0f\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u5b9a\u4e49Ray\u8fdc\u7a0b\u51fd\u6570\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>@ray.remote<\/code>\u88c5\u9970\u5668\uff1a<\/strong>\u5728\u9700\u8981\u5e76\u884c\u8fd0\u884c\u7684\u51fd\u6570\u4e0a\u4f7f\u7528<code>@ray.remote<\/code>\u88c5\u9970\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@ray.remote<\/p>\n<p>def my_function(x):<\/p>\n<p>    return x * x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528\u8fdc\u7a0b\u51fd\u6570\uff1a<\/strong>\u4f7f\u7528<code>.remote<\/code>\u65b9\u6cd5\u8c03\u7528\u8fdc\u7a0b\u51fd\u6570\u3002\u8c03\u7528\u8fdc\u7a0b\u51fd\u6570\u65f6\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2aRay\u5bf9\u8c61\uff08Ray Future\uff09\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = my_function.remote(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u7ed3\u679c\uff1a<\/strong>\u4f7f\u7528<code>ray.get()<\/code>\u51fd\u6570\u83b7\u53d6\u8fdc\u7a0b\u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result_value = ray.get(result)<\/p>\n<p>print(result_value)  # \u8f93\u51fa25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u8c03\u7528Ray\u8fdc\u7a0b\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u4e86Ray\u8fdc\u7a0b\u51fd\u6570\u4e4b\u540e\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u5b9e\u73b0\u5e76\u884c\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u8c03\u7528Ray\u8fdc\u7a0b\u51fd\u6570\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u591a\u4e2a\u8fdc\u7a0b\u51fd\u6570\uff1a<\/strong>\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u8fdc\u7a0b\u51fd\u6570\uff0c\u4ee5\u4fbf\u5728\u4ee3\u7801\u4e2d\u5b9e\u73b0\u590d\u6742\u7684\u5e76\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@ray.remote<\/p>\n<p>def add(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>@ray.remote<\/p>\n<p>def multiply(x, y):<\/p>\n<p>    return x * y<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u884c\u8c03\u7528\u8fdc\u7a0b\u51fd\u6570\uff1a<\/strong>\u4f7f\u7528<code>.remote<\/code>\u65b9\u6cd5\u5e76\u884c\u8c03\u7528\u591a\u4e2a\u8fdc\u7a0b\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">add_result = add.remote(2, 3)<\/p>\n<p>multiply_result = multiply.remote(4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u5e76\u884c\u8ba1\u7b97\u7ed3\u679c\uff1a<\/strong>\u4f7f\u7528<code>ray.get()<\/code>\u51fd\u6570\u83b7\u53d6\u5e76\u884c\u8ba1\u7b97\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">add_value = ray.get(add_result)<\/p>\n<p>multiply_value = ray.get(multiply_result)<\/p>\n<p>print(f&quot;Add Result: {add_value}, Multiply Result: {multiply_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u83b7\u53d6\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6Ray\u8fdc\u7a0b\u51fd\u6570\u7684\u6267\u884c\u7ed3\u679c\u662f\u5e76\u884c\u8ba1\u7b97\u7684\u6700\u540e\u4e00\u6b65\u3002\u4ee5\u4e0b\u662f\u83b7\u53d6\u7ed3\u679c\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528<code>ray.get()<\/code>\u51fd\u6570\u7b49\u5f85\u6240\u6709\u4efb\u52a1\u5b8c\u6210\uff0c\u5e76\u83b7\u53d6\u6240\u6709\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">results = ray.get([add_result, multiply_result])<\/p>\n<p>print(f&quot;Results: {results}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u7ed3\u679c\uff1a<\/strong>\u53ef\u4ee5\u5bf9\u83b7\u53d6\u7684\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">add_value, multiply_value = results<\/p>\n<p>print(f&quot;Add Result: {add_value}, Multiply Result: {multiply_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0a\u662f\u8c03\u7528Python\u4e2d\u7684RayPy\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002\u901a\u8fc7\u5b89\u88c5Ray\u5e93\u3001\u521d\u59cb\u5316Ray\u3001\u5b9a\u4e49Ray\u8fdc\u7a0b\u51fd\u6570\u3001\u8c03\u7528Ray\u8fdc\u7a0b\u51fd\u6570\u4ee5\u53ca\u83b7\u53d6\u7ed3\u679c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5e76\u884c\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u548c\u6027\u80fd\u3002Ray\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684\u63a5\u53e3\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u5e76\u884c\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u8fdb\u4e00\u6b65\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h3>\u516d\u3001Ray\u4efb\u52a1\u4f9d\u8d56\u5173\u7cfb\u548c\u8c03\u5ea6<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cRay\u5141\u8bb8\u5b9a\u4e49\u4efb\u52a1\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u4ece\u800c\u5b9e\u73b0\u590d\u6742\u7684\u4efb\u52a1\u8c03\u5ea6\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u5177\u6709\u4f9d\u8d56\u5173\u7cfb\u7684\u8fdc\u7a0b\u51fd\u6570\uff1a<\/strong>\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u8fdc\u7a0b\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u8c03\u7528\u4e2d\u4f20\u9012\u4e4b\u524d\u4efb\u52a1\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@ray.remote<\/p>\n<p>def preprocess(data):<\/p>\n<p>    # \u6570\u636e\u9884\u5904\u7406\u903b\u8f91<\/p>\n<p>    return processed_data<\/p>\n<p>@ray.remote<\/p>\n<p>def analyze(processed_data):<\/p>\n<p>    # \u6570\u636e\u5206\u6790\u903b\u8f91<\/p>\n<p>    return analysis_result<\/p>\n<p>@ray.remote<\/p>\n<p>def postprocess(analysis_result):<\/p>\n<p>    # \u6570\u636e\u540e\u5904\u7406\u903b\u8f91<\/p>\n<p>    return final_result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528\u5177\u6709\u4f9d\u8d56\u5173\u7cfb\u7684\u8fdc\u7a0b\u51fd\u6570\uff1a<\/strong>\u5728\u8c03\u7528\u8fdc\u7a0b\u51fd\u6570\u65f6\uff0c\u4f20\u9012\u4e4b\u524d\u4efb\u52a1\u7684Ray\u5bf9\u8c61\uff08Ray Future\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>processed_data = preprocess.remote(data)<\/p>\n<p>analysis_result = analyze.remote(processed_data)<\/p>\n<p>final_result = postprocess.remote(analysis_result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u6700\u7ec8\u7ed3\u679c\uff1a<\/strong>\u4f7f\u7528<code>ray.get()<\/code>\u51fd\u6570\u83b7\u53d6\u6700\u7ec8\u4efb\u52a1\u7684\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">final_value = ray.get(final_result)<\/p>\n<p>print(f&quot;Final Result: {final_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001Ray Actor\u6a21\u578b<\/h3>\n<\/p>\n<p><p>Ray\u652f\u6301Actor\u6a21\u578b\uff0c\u53ef\u4ee5\u7528\u6765\u7ba1\u7406\u6709\u72b6\u6001\u7684\u4efb\u52a1\u3002Actor\u6a21\u578b\u5141\u8bb8\u521b\u5efa\u6709\u72b6\u6001\u7684\u8fdc\u7a0b\u5bf9\u8c61\uff0c\u5e76\u5728\u591a\u4e2a\u4efb\u52a1\u4e4b\u95f4\u5171\u4eab\u72b6\u6001\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49Actor\u7c7b\uff1a<\/strong>\u4f7f\u7528<code>@ray.remote<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u4e00\u4e2aActor\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@ray.remote<\/p>\n<p>class Counter:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.count = 0<\/p>\n<p>    def increment(self):<\/p>\n<p>        self.count += 1<\/p>\n<p>        return self.count<\/p>\n<p>    def get_count(self):<\/p>\n<p>        return self.count<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaActor\u5b9e\u4f8b\uff1a<\/strong>\u4f7f\u7528<code>.remote<\/code>\u65b9\u6cd5\u521b\u5efaActor\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter = Counter.remote()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528Actor\u65b9\u6cd5\uff1a<\/strong>\u4f7f\u7528<code>.remote<\/code>\u65b9\u6cd5\u8c03\u7528Actor\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter.increment.remote()<\/p>\n<p>current_count = ray.get(counter.get_count.remote())<\/p>\n<p>print(f&quot;Current Count: {current_count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001Ray\u96c6\u7fa4\u914d\u7f6e\u548c\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>Ray\u652f\u6301\u5728\u591a\u53f0\u673a\u5668\u4e0a\u8fd0\u884c\uff0c\u4ee5\u5b9e\u73b0\u5206\u5e03\u5f0f\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u914d\u7f6e\u548c\u7ba1\u7406Ray\u96c6\u7fa4\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u914d\u7f6eRay\u96c6\u7fa4\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2aRay\u96c6\u7fa4\u914d\u7f6e\u6587\u4ef6\uff0c\u6307\u5b9a\u96c6\u7fa4\u7684\u8282\u70b9\u3001\u8d44\u6e90\u548c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">cluster_name: example-cluster<\/p>\n<p>min_workers: 2<\/p>\n<p>max_workers: 10<\/p>\n<p>head_node:<\/p>\n<p>  InstanceType: m5.large<\/p>\n<p>  KeyName: your-key<\/p>\n<p>worker_nodes:<\/p>\n<p>  InstanceType: m5.large<\/p>\n<p>  KeyName: your-key<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u542f\u52a8Ray\u96c6\u7fa4\uff1a<\/strong>\u4f7f\u7528Ray\u7684\u547d\u4ee4\u884c\u5de5\u5177\u542f\u52a8Ray\u96c6\u7fa4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ray up cluster.yaml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fde\u63a5Ray\u96c6\u7fa4\uff1a<\/strong>\u5728Python\u4ee3\u7801\u4e2d\u8fde\u63a5\u5230Ray\u96c6\u7fa4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ray<\/p>\n<p>ray.init(address=&#39;auto&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ba1\u7406Ray\u96c6\u7fa4\uff1a<\/strong>\u4f7f\u7528Ray\u7684\u547d\u4ee4\u884c\u5de5\u5177\u7ba1\u7406Ray\u96c6\u7fa4\uff0c\u5982\u6dfb\u52a0\u8282\u70b9\u3001\u79fb\u9664\u8282\u70b9\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ray exec cluster.yaml &quot;ray stop&quot;<\/p>\n<p>ray down cluster.yaml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001Ray\u6570\u636e\u6d41\u548c\u6570\u636e\u5e76\u884c<\/h3>\n<\/p>\n<p><p>Ray\u652f\u6301\u6570\u636e\u6d41\u548c\u6570\u636e\u5e76\u884c\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u636e\u96c6\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u6570\u636e\u6d41\u5904\u7406\u51fd\u6570\uff1a<\/strong>\u4f7f\u7528<code>@ray.remote<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u6570\u636e\u6d41\u5904\u7406\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@ray.remote<\/p>\n<p>def process_chunk(chunk):<\/p>\n<p>    # \u6570\u636e\u5904\u7406\u903b\u8f91<\/p>\n<p>    return processed_chunk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u884c\u5904\u7406\u6570\u636e\u6d41\uff1a<\/strong>\u5c06\u6570\u636e\u96c6\u5206\u5272\u6210\u591a\u4e2a\u6570\u636e\u5757\uff0c\u5e76\u4f7f\u7528Ray\u5e76\u884c\u5904\u7406\u6bcf\u4e2a\u6570\u636e\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_chunks = [data[i:i+chunk_size] for i in range(0, len(data), chunk_size)]<\/p>\n<p>processed_chunks = [process_chunk.remote(chunk) for chunk in data_chunks]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u5e76\u884c\u5904\u7406\u7ed3\u679c\uff1a<\/strong>\u4f7f\u7528<code>ray.get()<\/code>\u51fd\u6570\u83b7\u53d6\u6240\u6709\u6570\u636e\u5757\u7684\u5904\u7406\u7ed3\u679c\uff0c\u5e76\u5408\u5e76\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">processed_data = ray.get(processed_chunks)<\/p>\n<p>final_data = combine_chunks(processed_data)<\/p>\n<p>print(f&quot;Final Data: {final_data}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u3001Ray\u8c03\u8bd5\u548c\u76d1\u63a7<\/h3>\n<\/p>\n<p><p>Ray\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8c03\u8bd5\u548c\u76d1\u63a7\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u76d1\u63a7Ray\u96c6\u7fa4\u7684\u72b6\u6001\u548c\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528Ray Dashboard\uff1a<\/strong>Ray Dashboard\u662f\u4e00\u4e2aWeb\u754c\u9762\uff0c\u53ef\u4ee5\u7528\u6765\u76d1\u63a7Ray\u96c6\u7fa4\u7684\u72b6\u6001\u548c\u6027\u80fd\u3002\u5728\u542f\u52a8Ray\u96c6\u7fa4\u65f6\uff0cRay Dashboard\u4f1a\u81ea\u52a8\u542f\u52a8\uff0c\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">ray dashboard<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165<code>http:\/\/localhost:8265<\/code>\uff0c\u5373\u53ef\u8bbf\u95eeRay Dashboard\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Ray Logging\uff1a<\/strong>Ray\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u5fd7\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u8bb0\u5f55Ray\u96c6\u7fa4\u548c\u4efb\u52a1\u7684\u65e5\u5fd7\u3002\u53ef\u4ee5\u5728Ray\u521d\u59cb\u5316\u65f6\u6307\u5b9a\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ray.init(log_to_driver=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Ray Profiling\uff1a<\/strong>Ray\u63d0\u4f9b\u4e86Profiling\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5206\u6790Ray\u4efb\u52a1\u7684\u6027\u80fd\u3002\u53ef\u4ee5\u5728\u8fdc\u7a0b\u51fd\u6570\u4e2d\u4f7f\u7528<code>ray.profile<\/code>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@ray.remote<\/p>\n<p>def my_function(x):<\/p>\n<p>    with ray.profile(&quot;my_function&quot;):<\/p>\n<p>        # \u8ba1\u7b97\u903b\u8f91<\/p>\n<p>        return x * x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e00\u3001Ray\u4e0e\u5176\u4ed6\u6846\u67b6\u7684\u96c6\u6210<\/h3>\n<\/p>\n<p><p>Ray\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6846\u67b6\u96c6\u6210\uff0c\u5982TensorFlow\u3001PyTorch\u7b49\uff0c\u4ee5\u5b9e\u73b0\u5206\u5e03\u5f0f<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6df1\u5ea6\u5b66\u4e60\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u96c6\u6210TensorFlow\uff1a<\/strong>Ray\u63d0\u4f9b\u4e86<code>tf.keras<\/code>\u7684\u5206\u5e03\u5f0f\u8bad\u7ec3\u652f\u6301\uff0c\u53ef\u4ee5\u7528\u6765\u5e76\u884c\u8bad\u7ec3TensorFlow\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>import ray<\/p>\n<p>from ray.util.tf.keras import prepare_model, prepare_dataset<\/p>\n<p>ray.init()<\/p>\n<h2><strong>\u5b9a\u4e49\u6a21\u578b<\/strong><\/h2>\n<p>model = tf.keras.models.Sequential([<\/p>\n<p>    tf.keras.layers.Dense(10, activation=&#39;relu&#39;),<\/p>\n<p>    tf.keras.layers.Dense(1, activation=&#39;sigmoid&#39;)<\/p>\n<p>])<\/p>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;binary_crossentropy&#39;)<\/p>\n<h2><strong>\u51c6\u5907\u6570\u636e\u96c6<\/strong><\/h2>\n<p>dataset = tf.data.Dataset.from_tensor_slices((x_tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n, y_train))<\/p>\n<p>dataset = dataset.batch(32)<\/p>\n<p>dataset = prepare_dataset(dataset)<\/p>\n<h2><strong>\u5206\u5e03\u5f0f\u8bad\u7ec3<\/strong><\/h2>\n<p>model = prepare_model(model)<\/p>\n<p>model.fit(dataset, epochs=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u96c6\u6210PyTorch\uff1a<\/strong>Ray\u63d0\u4f9b\u4e86<code>torch<\/code>\u7684\u5206\u5e03\u5f0f\u8bad\u7ec3\u652f\u6301\uff0c\u53ef\u4ee5\u7528\u6765\u5e76\u884c\u8bad\u7ec3PyTorch\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>import ray<\/p>\n<p>from ray.util.sgd.torch import TorchTrainer, TrainingOperator<\/p>\n<p>ray.init()<\/p>\n<p>class MyTrainingOperator(TrainingOperator):<\/p>\n<p>    def setup(self, config):<\/p>\n<p>        model = torch.nn.Linear(1, 1)<\/p>\n<p>        optimizer = torch.optim.SGD(model.parameters(), lr=0.1)<\/p>\n<p>        self.model, self.optimizer = self.register(models=model, optimizers=optimizer)<\/p>\n<p>trainer = TorchTrainer(<\/p>\n<p>    training_operator_cls=MyTrainingOperator,<\/p>\n<p>    num_workers=4,<\/p>\n<p>    use_gpu=True<\/p>\n<p>)<\/p>\n<p>trainer.train()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e8c\u3001Ray\u9ad8\u7ea7\u7279\u6027<\/h3>\n<\/p>\n<p><p>Ray\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7279\u6027\uff0c\u5982Ray Tune\u3001Ray Serve\u7b49\uff0c\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u8d85\u53c2\u6570\u8c03\u4f18\u3001\u6a21\u578b\u670d\u52a1\u7b49\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Ray Tune\uff1a<\/strong>Ray Tune\u662f\u4e00\u4e2a\u7528\u4e8e\u8d85\u53c2\u6570\u8c03\u4f18\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u81ea\u52a8\u641c\u7d22\u6700\u4f18\u8d85\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ray<\/p>\n<p>from ray import tune<\/p>\n<p>def train(config):<\/p>\n<p>    # \u6a21\u578b\u8bad\u7ec3\u903b\u8f91<\/p>\n<p>    for epoch in range(10):<\/p>\n<p>        # \u6a21\u62df\u8bad\u7ec3\u635f\u5931<\/p>\n<p>        loss = (config[&quot;lr&quot;] - 0.1)  2 + 0.01<\/p>\n<p>        tune.report(loss=loss)<\/p>\n<p>ray.init()<\/p>\n<p>analysis = tune.run(<\/p>\n<p>    train,<\/p>\n<p>    config={&quot;lr&quot;: tune.grid_search([0.01, 0.1, 0.2])}<\/p>\n<p>)<\/p>\n<p>print(&quot;Best config: &quot;, analysis.get_best_config(metric=&quot;loss&quot;, mode=&quot;min&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Ray Serve\uff1a<\/strong>Ray Serve\u662f\u4e00\u4e2a\u7528\u4e8e\u6a21\u578b\u670d\u52a1\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u90e8\u7f72\u548c\u7ba1\u7406\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ray<\/p>\n<p>from ray import serve<\/p>\n<p>ray.init()<\/p>\n<p>serve.start()<\/p>\n<p>@serve.deployment<\/p>\n<p>class Model:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        # \u6a21\u578b\u52a0\u8f7d\u903b\u8f91<\/p>\n<p>    def __call__(self, request):<\/p>\n<p>        # \u6a21\u578b\u9884\u6d4b\u903b\u8f91<\/p>\n<p>        return {&quot;result&quot;: &quot;prediction&quot;}<\/p>\n<p>Model.deploy()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e09\u3001Ray\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Ray\u8fdb\u884c\u5e76\u884c\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9bRay\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5408\u7406\u5212\u5206\u4efb\u52a1\uff1a<\/strong>\u5c06\u8ba1\u7b97\u4efb\u52a1\u5408\u7406\u5212\u5206\u4e3a\u591a\u4e2a\u5b50\u4efb\u52a1\uff0c\u4ee5\u4fbf\u5145\u5206\u5229\u7528Ray\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002\u4efb\u52a1\u7c92\u5ea6\u4e0d\u5b9c\u8fc7\u7ec6\u6216\u8fc7\u7c97\uff0c\u5e94\u6839\u636e\u8ba1\u7b97\u590d\u6742\u5ea6\u548c\u6570\u636e\u91cf\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u8fc7\u5ea6\u901a\u4fe1\uff1a<\/strong>\u5728\u5206\u5e03\u5f0f\u8ba1\u7b97\u4e2d\uff0c\u901a\u4fe1\u5f00\u9500\u53ef\u80fd\u4f1a\u6210\u4e3a\u6027\u80fd\u74f6\u9888\u3002\u5c3d\u91cf\u51cf\u5c11\u4efb\u52a1\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u8f93\uff0c\u907f\u514d\u9891\u7e41\u7684\u8fdc\u7a0b\u8c03\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5bf9\u8c61\u5b58\u50a8\uff1a<\/strong>Ray\u63d0\u4f9b\u4e86\u5bf9\u8c61\u5b58\u50a8\uff08Object Store\uff09\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u5927\u89c4\u6a21\u6570\u636e\u3002\u4f7f\u7528\u5bf9\u8c61\u5b58\u50a8\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u4f20\u8f93\u5f00\u9500\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76d1\u63a7\u548c\u8c03\u4f18\uff1a<\/strong>\u4f7f\u7528Ray Dashboard\u548cProfiling\u5de5\u5177\u76d1\u63a7\u4efb\u52a1\u6267\u884c\u60c5\u51b5\uff0c\u53ca\u65f6\u53d1\u73b0\u6027\u80fd\u74f6\u9888\u548c\u95ee\u9898\u3002\u6839\u636e\u76d1\u63a7\u7ed3\u679c\u8fdb\u884c\u8c03\u4f18\uff0c\u5982\u8c03\u6574\u4efb\u52a1\u5212\u5206\u3001\u4f18\u5316\u7b97\u6cd5\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5408\u7406\u914d\u7f6e\u8d44\u6e90\uff1a<\/strong>\u5728\u521d\u59cb\u5316Ray\u65f6\uff0c\u5408\u7406\u914d\u7f6eCPU\u548cGPU\u8d44\u6e90\uff0c\u786e\u4fdd\u8d44\u6e90\u7684\u6709\u6548\u5229\u7528\u3002\u907f\u514d\u8d44\u6e90\u8fc7\u5ea6\u5206\u914d\u6216\u4e0d\u8db3\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u56db\u3001Ray\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>Ray\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u5e76\u884c\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\u573a\u666f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5178\u578b\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff1a<\/strong>Ray\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u5982\u6570\u636e\u9884\u5904\u7406\u3001\u6570\u636e\u6e05\u6d17\u3001\u7279\u5f81\u63d0\u53d6\u7b49\u3002\u901a\u8fc7\u5e76\u884c\u8ba1\u7b97\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6570\u636e\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u5e03\u5f0f\u673a\u5668\u5b66\u4e60\uff1a<\/strong>Ray\u652f\u6301\u4e0eTensorFlow\u3001PyTorch\u7b49\u6846\u67b6\u96c6\u6210\uff0c\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u5206\u5e03\u5f0f\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u3002\u901a\u8fc7\u5206\u5e03\u5f0f\u8bad\u7ec3\uff0c\u53ef\u4ee5\u52a0\u901f\u6a21\u578b\u8bad\u7ec3\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d85\u53c2\u6570\u8c03\u4f18\uff1a<\/strong>Ray Tune\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8d85\u53c2\u6570\u8c03\u4f18\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u81ea\u52a8\u641c\u7d22\u6700\u4f18\u8d85\u53c2\u6570\uff0c\u63d0\u9ad8\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u578b\u670d\u52a1\uff1a<\/strong>Ray Serve\u53ef\u4ee5\u7528\u6765\u90e8\u7f72\u548c\u7ba1\u7406\u673a\u5668\u5b66\u4e60\u6a21\u578b\uff0c\u63d0\u4f9b\u9ad8\u6027\u80fd\u3001\u4f4e\u5ef6\u8fdf\u7684\u6a21\u578b\u670d\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u79d1\u5b66\u8ba1\u7b97\uff1a<\/strong>Ray\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u548c\u6a21\u62df\uff0c\u5982\u6c14\u5019\u6a21\u62df\u3001\u57fa\u56e0\u5206\u6790\u3001\u7269\u7406\u4eff\u771f\u7b49\u3002\u901a\u8fc7\u5e76\u884c\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e94\u3001Ray\u7684\u672a\u6765\u53d1\u5c55<\/h3>\n<\/p>\n<p><p>Ray\u4f5c\u4e3a\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u5f97\u5230\u4e86\u5e7f\u6cdb\u7684\u793e\u533a\u652f\u6301\u548c\u6301\u7eed\u7684\u53d1\u5c55\u3002\u4ee5\u4e0b\u662fRay\u672a\u6765\u7684\u4e00\u4e9b\u53d1\u5c55\u65b9\u5411\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u66f4\u591a\u6846\u67b6\u96c6\u6210\uff1a<\/strong>Ray\u5c06\u7ee7\u7eed\u4e0e\u66f4\u591a\u7684\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u96c6\u6210\uff0c\u5982MXNet\u3001Caffe\u7b49\uff0c\u4ee5\u652f\u6301\u66f4\u591a\u7684\u5206\u5e03\u5f0f\u8bad\u7ec3\u548c\u63a8\u7406\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<\/li>\n<\/ol>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Ray\u5e76\u5b89\u88c5raypy\u5e93\uff1f<\/strong><br \/>\u8981\u4f7f\u7528raypy\uff0c\u9996\u5148\u9700\u8981\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5Ray\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install ray<\/code>\u547d\u4ee4\u6765\u5b8c\u6210\u5b89\u88c5\u3002\u786e\u4fdd\u4f60\u7684Python\u7248\u672c\u4e3a3.6\u53ca\u4ee5\u4e0a\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165Ray\u5e93\u6765\u5f00\u59cb\u4f7f\u7528\uff0c\u6bd4\u5982\u4f7f\u7528<code>import ray<\/code>\u3002<\/p>\n<p><strong>Ray\u7684\u4e3b\u8981\u529f\u80fd\u548c\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>Ray\u662f\u4e00\u4e2a\u7528\u4e8e\u5e76\u884c\u548c\u5206\u5e03\u5f0f\u8ba1\u7b97\u7684\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u5e94\u7528\u573a\u666f\uff0c\u5305\u62ec\u673a\u5668\u5b66\u4e60\u3001\u6570\u636e\u5904\u7406\u548c\u5f3a\u5316\u5b66\u4e60\u7b49\u3002\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u8ba1\u7b97\u4efb\u52a1\u5206\u5e03\u5230\u591a\u4e2aCPU\u6216GPU\u4e0a\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u548c\u6027\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Ray\u4e2d\u521b\u5efa\u548c\u7ba1\u7406\u4efb\u52a1\uff1f<\/strong><br \/>\u5728Ray\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>@ray.remote<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u4e00\u4e2a\u8fdc\u7a0b\u51fd\u6570\u6216\u7c7b\uff0c\u4f7f\u5176\u80fd\u591f\u5728\u96c6\u7fa4\u4e2d\u7684\u4e0d\u540c\u5de5\u4f5c\u8282\u70b9\u4e0a\u6267\u884c\u3002\u4f7f\u7528<code>ray.get()<\/code>\u53ef\u4ee5\u83b7\u53d6\u8fdc\u7a0b\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002\u901a\u8fc7\u8fd9\u4e9b\u529f\u80fd\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u5e76\u884c\u8ba1\u7b97\u4efb\u52a1\uff0c\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<p><strong>Ray\u4e0e\u5176\u4ed6\u5e76\u884c\u8ba1\u7b97\u6846\u67b6\u76f8\u6bd4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>Ray\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7075\u6d3b\u6027\u548c\u6613\u7528\u6027\u3002\u4e0e\u5176\u4ed6\u6846\u67b6\u76f8\u6bd4\uff0cRay\u652f\u6301\u52a8\u6001\u4efb\u52a1\u8c03\u5ea6\u548c\u9ad8\u6548\u7684<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u65b9\u4fbf\u5730\u6784\u5efa\u590d\u6742\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u3002\u6b64\u5916\uff0cRay\u8fd8\u80fd\u591f\u4e0e\u6d41\u884c\u7684\u673a\u5668\u5b66\u4e60\u5e93\uff08\u5982TensorFlow\u548cPyTorch\uff09\u65e0\u7f1d\u96c6\u6210\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u5de5\u5177\u548c\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528RayPy\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a\u5b89\u88c5Ray\u5e93\u3001\u521d\u59cb\u5316Ray\u3001\u5b9a\u4e49Ray\u8fdc\u7a0b\u51fd\u6570\u3001\u8c03\u7528R [&hellip;]","protected":false},"author":3,"featured_media":1174442,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174436"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1174436"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174436\/revisions"}],"predecessor-version":[{"id":1174444,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174436\/revisions\/1174444"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174442"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}