我可以从笔记本电脑重启iPython集群吗?

时间:2016-12-22 14:58:55

标签: python jupyter ipython-parallel

我只是想知道是否有一些我可以执行的python代码或魔法会重启ipython群集。似乎每次我更改代码时,都需要重新启动。

1 个答案:

答案 0 :(得分:0)

一个简单的想法,似乎对生产来说太“黑客”了:

设置Client并定义一个简单的测试功能。

import ipyparallel as ipp
c = ipp.Client()
dv = c[:]

# simple function
@dv.remote(block=True)
def getpid():
    import os
    return os.getpid()

getpid()
[1994, 1995, 1998, 2001]

定义重启群集的功能。 targets='all'hub=True的{​​{3}}会杀死整个群集。然后使用!shutdown magic命令启动新群集。

import time
def restart_ipcluster(client):
    client.shutdown(targets='all', hub=True)
    time.sleep(5) # give the cluster a few seconds to shutdown

    # include other args as necessary
    !ipcluster start -n4 --daemonize
    time.sleep(60) # give cluster ~min to start

    return ipp.Client() # with keyword args as necessary

这种方法的一个缺点是需要重新分配DirectView,并且需要重新执行用dv.remotedv.parallel修饰的任何功能。

c = restart_ipcluster(c)    
dv = c[:]

@dv.remote(block=True)
def getpid():
    import os
    return os.getpid()

getpid()
[3620, 3621, 3624, 3627]

阅读ipyparallel Client的来源,上面提到的shutdown方法有一个关键字参数restart=False,但它目前尚未实现。也许开发人员正在研究一种可靠的方法。