【问题标题】:Starting ipcluster from code从代码启动 ipcluster
【发布时间】:2017-11-28 20:16:01
【问题描述】:

我想从我的 Jupyter 笔记本动态启动集群以实现特定功能。虽然我可以启动集群并让引擎运行,但我遇到了两个问题:

(1) 我无法在后台运行 ipcluster 命令。当我通过笔记本运行命令时,单元一直运行到集群运行时,即我无法在同一个笔记本中运行更多单元。一旦引擎在不同的笔记本中启动,我就可以使用它们。如何在后台运行 ipcluster?

(2) 我的代码总是启动 8 个引擎,不管 ipcluster_config.py 中的设置如何。

代码:

server_num = 3
ip_new = '10.0.1.' + str(10+server_num)
cluster_profile = "ssh" + str(server_num)

import commands
import time
commands.getstatusoutput("ipython profile create --parallel --profile=" + cluster_profile)

text = """
c.SSHEngineSetLauncher.engines = {'""" +ip_new + """' : 12}
c.LocalControllerLauncher.controller_args = ["--ip=10.0.1.163"]
c.SSHEngineSetLauncher.engine_cmd = ['/home/ubuntu/anaconda2/pkgs/ipyparallel-6.0.2-py27_0/bin/ipengine']
"""

with open("/home/ubuntu/.ipython/profile_" + cluster_profile + "/ipcluster_config.py", "a") as myfile:
    myfile.write(text)

result = commands.getstatusoutput("(ipcluster start --profile='"+ cluster_profile+"' &)")
time.sleep(120)
print(result[1])

【问题讨论】:

  • 我在运行命令!ipcluster start 后遇到了类似的问题,笔记本内核仍然很忙。但我注意到,只需刷新页面,我就可以运行以释放内核并运行其他单元。你找到解决办法了吗?

标签: ipython ipython-parallel


【解决方案1】:

当我在 StackOverflow 上看到你的答案无人回答时,我差点心脏病发作,因为我有同样的问题。

但是运行

ipcluster start --help 

命令显示:

--daemonize

这使它在后台运行。

所以在你的笔记本中你可以这样做:

no_engines = 6
!ipcluster start -n {no_engines} --daemonize

注意:根据

,这在 Windows 上不起作用
ipcluster start --help

【讨论】:

  • 抱歉回复晚了!
【解决方案2】:

我不熟悉commands 模块的详细信息(根据https://docs.python.org/2/library/commands.html,它自2.6 以来已被弃用)但我知道使用subprocess 模块捕获输出将使解释器阻塞,直到系统调用完成。

此外,如果您使用ipcluster 命令,即使不调整配置​​文件,也可以从命令行设置引擎的数量。所以,这样的事情对我有用:

from ipyparallel import Client
import subprocess

nengines = 3 # or whatever
subprocess.Popen(["ipcluster", "start", "-n={:d}".format(nengines)])
rc = Client()
# send your jobs to the engines; when done do
subprocess.Popen(["ipcluster", "stop"])

当然,这并不能解决动态添加或删除主机的问题(从您的代码中看起来您可能正在尝试这样做),但如果您只关心有多少主机可用,而不是哪个主机可用那些,您可以创建一个默认的 ipcluster 配置,其中包括所有可能的主机,并通过类似于上面的代码根据需要分配它们。

另请注意,ipcluster 启动可能需要一两秒钟的时间,因此您可能希望在您的第一个 subprocess.Popen 调用和尝试生成客户端之间添加一个 time.sleep 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多