首页 > 解决方案 > ray中的Cython类初始化

问题描述

我正在使用 Ray ( https://github.com/ray-project/ray ) 和 Cython 0.29 来并行化一些现有代码,我决定使用我的 Cython 函数之一定义一个 cdef 类作为其方法来简化运行并行的多个Actor的代码。我遇到的问题是,当我在 cython_simple.py (example6 )之后创建我的类的实例时,我得到一个“__init__ 错误的参数太多”。

我有几个问题,但第一个问题是当我的类实例被 ray.remote 修饰时,如何正确地为其提供参数。

我一直在尝试这种方法,其中 Test 是导入的类:

    import ray
    from cython_test.cython_test import Test
    ray.init()
    Test1 = ray.remote(Test)
    #Instatiate an actor
    args = (img, 10, 10)
    a1 = Test1.remote(*args)

但我回来了:

    Traceback (most recent call last):
      File "test_ray_cython.py", line 25, in <module>
        a1 = Test1.remote(*args)
      File "/local/data/home/gmosby/.local/lib/python3.7/site-packages/ray/actor.py", line 282, in remote
        return self._remote(args=args, kwargs=kwargs)
      File "/local/data/home/gmosby/.local/lib/python3.7/site-packages/ray/actor.py", line 384, in _remote
        kwargs)
      File "/local/data/home/gmosby/.local/lib/python3.7/site-packages/ray/signature.py", line 221, in extend_args
        .format(function_name))
    Exception: Too many arguments were passed to the function '__init__'

附加信息:这里的 Cython 类被初始化为def __init__(self, img, nx, ny)

如果我想将 Cython 类与 Ray 一起使用,它是否必须没有任何初始化参数?

发现的解决方法:在 __init__ 方法中使用位置参数装饰 Cython 类确实失败了,但我决定将一些类成员的设置移动到单独的函数中。在此处查看此问题的说明和解决方法 ( https://github.com/astrophysaxist/cython_test )

标签: pythonclasscythonray

解决方案


发现的解决方法:在 __init__ 方法中使用位置参数装饰 Cython 类确实失败了,但我决定将一些类成员的设置移动到单独的函数中。在此处查看此问题的说明和解决方法 ( https://github.com/astrophysaxist/cython_test )


推荐阅读