我创建了Cython扩展类型,我想在nogil上下文中使用它。但是编译器总是抛出错误。这是我要执行的操作的一个简单示例:
1 # cython: language_level=3
2
3 from cython.parallel import prange
4
5 cdef class C1:
6
7 cdef public:
8 int val
9
10 def __cinit__(self, value):
11 self.val = value
12
13 def iterate_C1():
14
15 cdef int i
16 cdef int N = 4
17 cdef C1 c1_i
18
19 # This compiles fine
20 c1 = C1(4)
21 print(f'c1.val={c1.val}')
22
23 # But this doesn't
24 with nogil:
25 for i in prange(N):
26 c1_i = C1(i)
我有很多例外,但它们都看起来像这样:
Compiling c_one.pyx because it changed.
[1/1] Cythonizing c_one.pyx
Error compiling Cython file:
------------------------------------------------------------
...
print(f'c1.val={c1.val}')
# But this doesn't
with nogil:
for i in prange(N):
c1_i = C1(i)
^
------------------------------------------------------------
c_one.pyx:26:12: Assignment of Python object not allowed without gil
Error compiling Cython file:
------------------------------------------------------------
...
print(f'c1.val={c1.val}')
# But this doesn't
with nogil:
for i in prange(N):
c1_i = C1(i)
^
------------------------------------------------------------
c_one.pyx:26:21: Calling gil-requiring function not allowed without gil
所以在nogil上下文中无法使用Cython扩展类型?
您绝对不能在cdef
块内创建nogil
扩展类型的实例。它们最终是Python对象,需要引用计数(包括在某些非显而易见的地方,例如类型对象),Python管理的内存分配以及ci
之前内容的重新分配,包括调用其析构函数。 >
您应该能够访问其cdef members
(例如,此类的val
)并调用其标记为cdef
的nogil
函数。