Search code examples
pythonperformanceparallel-processingcythonray

ray in Cython error: The @ray.remote decorator must be applied to either a function or to a class


I apply ray with Cython. It shows an error: TypeError: The @ray.remote decorator must be applied to either a function or to a class.

How to solve this problem? Thanks.

  1. The setup.py:
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=cythonize("ray_cython.pyx"),
    include_dirs=[numpy.get_include()]
)

setup(
    ext_modules=[
        Extension("ray_cython", ["ray_cython.c"],
                  include_dirs=[numpy.get_include()]),
    ],
)

# compile by  python setup.py build_ext --inplace
# Then run compare.py
  1. The ray_cython.pyx file:
from numpy import linalg as LA
import time
#Import package
import ray

cimport numpy as np
np.import_array()
DTYPE = np.int
ctypedef np.int_t DTYPE_t


@ray.remote
def myfunction():
...


Solution

  • https://docs.ray.io/en/latest/ray-core/advanced.html#cython-code-in-ray

    You must include the following two lines at the top of any *.pyx file:

    #!python
    # cython: embedsignature=True, binding=True
    

    You cannot decorate Cython functions within a *.pyx file (there are ways around this, but creates a leaky abstraction between Cython and Python that would be very challenging to support generally). Instead, prefer the following in your Python code:

    some_cython_func = ray.remote(some_cython_module.some_cython_func)