相关疑难解决方法(0)

我需要在 Cython 中使用 `nogil`

我有一些想要尽快运行的 Cython 代码。我是否需要释放 GIL 才能执行此操作?

假设我的代码与此类似:

import numpy as np

# trivial definition just for illustration!
cdef double some_complicated_function(double x) nogil:
    return x

cdef void func(double[:] input) nogil:
    cdef double[:] array = np.zeros_like(input)
    for i in range(array.shape[0]):
        array[i] = some_complicated_function(input[i])
Run Code Online (Sandbox Code Playgroud)

我从np.zeros_like类似于以下内容的行中收到大量错误消息:

nogilcode.pyx:7:40: Calling gil-requiring function not allowed without gil
nogilcode.pyx:7:29: Accessing Python attribute not allowed without gil
nogilcode.pyx:7:27: Accessing Python global or builtin not allowed without gil
nogilcode.pyx:7:40: Constructing Python tuple not allowed without gil
nogilcode.pyx:7:41: Converting to …
Run Code Online (Sandbox Code Playgroud)

python cython gil

4
推荐指数
1
解决办法
988
查看次数

标签 统计

cython ×1

gil ×1

python ×1