是否可以在 nogil 部分中使用 Cython 扩展类型?
Is it possible to use a Cython Extension Type in a nogil section?
我已经创建了一个 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 扩展类型是不可能的吗?
您绝对不能在 nogil
块中创建 cdef
扩展类型的实例。它们最终是 Python 对象,需要引用计数(包括在一些不明显的地方,例如类型对象),分配 Python 管理的内存,以及释放 ci
,包括调用它的析构函数。
您应该能够访问他们的 cdef members
(例如 val
对于这个 class)并调用他们的 cdef
标记为 nogil
的函数.
我已经创建了一个 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 扩展类型是不可能的吗?
您绝对不能在 nogil
块中创建 cdef
扩展类型的实例。它们最终是 Python 对象,需要引用计数(包括在一些不明显的地方,例如类型对象),分配 Python 管理的内存,以及释放 ci
,包括调用它的析构函数。
您应该能够访问他们的 cdef members
(例如 val
对于这个 class)并调用他们的 cdef
标记为 nogil
的函数.