代码之家  ›  专栏  ›  技术社区  ›  matanster

Juypter中带有cython的Dask:ModuleNotFoundError:没有名为'\u cython_magic的模块

  •  0
  • matanster  · 技术社区  · 7 年前

    我得到:

    我读过 explanation 关于后一条错误消息,但这与stacktrace顶部的错误消息一起出现是令人困惑的:

    distributed.utils-错误-工作进程已存在tcp://127.0.0.1:35780

    实际错误通过管道传输到运行 Jupyter notebook 我笔记本的命令:

    ModuleNotFoundError:没有名为“\u cython\u magic\u faba6120a194ab58ae9efd1da474433f”的模块

    所以我会自己研究如何解决这个问题,现在我发现了我的案例中的详细错误。一个关于这种特殊配置的精确提示会很好,但是我想在笔记本外将所有cython代码提取为python代码比让dask了解cython魔法命令更明智?

    1 回复  |  直到 7 年前
        1
  •  1
  •   P.Toccaceli    6 年前

    下面是一个完整的玩具示例(使用SLURM集群在JupyterLab上测试)。 这个例子使用Cython编译一个简单的函数,它对两个整数求和,但是当然可以将相同的技术应用到复杂(而且更有用)的代码中。
    这里的关键技巧是必须让工人找到并导入Cython库。
    这需要导入 pyximport pyximport.install() ,然后在每个工作进程上导入Cython生成的模块。这是用 register_worker_callback() . 注意,Cython生成的模块位于 <IPYTHONCACHEDIR/cython 目录( IPYTHONCACHEDIR IPython.paths.get_ipython_cache_dir() ). 必须将目录添加到Python查找模块的路径中,以便可以加载Cython生成的模块。
    本例假设为SLURM,但只是为了方便起见。 分布式“网络”可以用任何其他方法设置(参见 http://distributed.dask.org/en/latest/setup.html ).

    from dask import delayed
    
    %load_ext cython
    
    # Create a toy Cython function and put it into a module named remoteCython
    %%cython -n remoteCython
    def cython_sum(int a, int b):
        return a+b
    
    # Set up a distributed cluster (minimal, just for illustration)
    # I use SLURM.
    from dask_jobqueue import SLURMCluster
    from distributed import Client
    
    cluster = SLURMCluster(memory="1GB",
                           processes=1,
                           cores=1,
                           walltime="00:10:00")
    
    cluster.start_workers(1)   # Start as many workers as needed.
    
    
    
    client = Client(cluster)
    
    def init_pyx(dask_worker):
        import pyximport
        pyximport.install()
    
        import sys
        sys.path.insert(0,'<IPYTHONCACHEDIR>/cython/')   # <<< replace <IPYTHONCACHEDIR> as appropriate
    
        import remoteCython
    
    client.register_worker_callbacks(init_pyx)  # This runs init_pyx() on any Worker at init
    
    import remoteCython
    
    # ASIDE: you can find where the full path of Cython-generated library by
    # looking at remoteCython.__file__
    
    # The following creates a task and submits to the scheduler.
    # The task computes the sum of 123 and 321 via the Cython function defined above
    future = client.compute(delayed(remoteCython.cython_sum)(123,321)) 
    
    # The task is executed on the remote worker
    
    # We fetch the result from the remote worker
    print(future.result())   # This prints 444
    
    # We're done. Let's release the SLURM jobs.
    cluster.close()
    
        2
  •  1
  •   mdurant    7 年前

    %%cython ,创建并构建一个临时扩展,结束导入本地(客户端)会话,而不安装到python环境中。我不确定这到底是怎么回事。

    你至少应该确保你创建了你的客户 你编译你的cython细胞,然后他们可能会继承所需的环境,但有一个很好的机会,猴子修补的细胞魔术是太复杂的工作,在任何情况下。