【问题标题】:Reload module on remote ipengine when using ipython使用 ipython 时在远程 ipengine 上重新加载模块
【发布时间】:2025-12-30 12:15:11
【问题描述】:

希望这有一个我在阅读文档时错过的直截了当的答案。以下是问题-

  1. 我在启动时在所有 ipengine(s) 上加载了一个模块
  2. 我已经对模块进行了更改
  3. 我希望将这些更改传播到远程 ipengine,即我希望在所有远程实例中重新加载模块

如何做到这一点?

【问题讨论】:

    标签: python parallel-processing ipython


    【解决方案1】:

    您还可以通过以下方式打开引擎上的 ipython 自动重载功能:

    %px %load_ext autoreload
    %px %autoreload 2
    

    请注意,此解决方案和使用 dview.execute() 调用 reload 在新引擎稍后可以上线时(例如在集群上使用批处理调度程序时)都会出现问题:它们仅在当前存在的引擎上执行。

    另一个问题:您可能需要深度(递归)重新加载。看到这个选项到 ipengine:

    --ZMQInteractiveShell.deep_reload=<CBool>
    Default: False
    Enable deep (recursive) reloading by default. IPython can use the
    deep_reload module which reloads changes in modules recursively (it replaces
    the reload() function, so you don't need to change anything to use it).
    deep_reload() forces a full reload of modules whose code may have changed,
    which the default reload() function does not.  When deep_reload is off,
    IPython will use the normal reload(), but deep_reload will still be
    available as dreload().
    

    【讨论】:

    • 这真的有效吗?我正在使用它,但没有重新加载或 deep_reloaded
    【解决方案2】:

    这是我找到的答案,不确定这是不是最好的方法

    from IPython.parallel import Client
    rc = Client(profile='ssh')
    dview = rc[:]
    dview.execute('reload(<module>)', block = True)
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我正在开发一个我想在远程引擎上测试的模块,但我不想将我的更改提交给 git,然后在每次之前拉动引擎机器上的更改远程重载。

      可能有更好的方法来做到这一点,但我的解决方案是编写a simple helper module,这样可以很容易地通过 scp 将正在进行的代码传送到引擎。

      我将在这里复制使用示例:

      import IPython.parallel
      import ishuttle
      import my_module as mm
      
      # Create a client for the cluster with remote engines
      rc = IPython.parallel.Client(profile='remote_ssh_engines')
      dview = rc[:]
      
      # Create a shuttle object; the engines' working directory
      # is switched to '/Remote/engine/server/scratch' after its creation
      s = ishuttle.Shuttle(rc, '/Remote/engine/server/scratch')
      
      # Make my_module available on all engines as mm. This code scp's the
      # module over, imports it as mm, then reloads it.
      s.remote_import('my_module', import_as='mm')
      
      # Apply our favourite function from our favourite module
      dview.apply_sync(mm.my_func, 'favourite argument for my_func')
      

      【讨论】:

        最近更新 更多