from cython.parallel import prange
@cython.boundscheck(False) # Deactivate bounds checking
@cython.wraparound(False) # Deactivate negative indexing.
cpdef int find_indices(int[::1] values, int[::1] source, int[::1] rep, int lim):
cdef int num_values = values[:lim]
cdef int num_source = source.shape[0]
i:int
for i in prange(num_values, nogil=True):
j:int
for j in prange(num_source):
if source[j] == values[i]:
if rep[j] <10: return -1
return 1
def main():
values:int[::1] = np.array([2, 3, 1, 5, 4])
search_array:int[::1] = np.array([1, 2, 3, 4, 5])
rep_array:int[::1] = np.array([11, 15, 18, 3, 2])
limit=3
result_index = find_indices(values, search_array, rep_array, limit)
if result_index == 1:
print(f"Valid {result_index}")
else:
print("No valid.")
if rep[j] <10: return -1
cyt.pyx:347:22: Indexing Python object not allowed without gil
嗨,我想知道如何返回 -1 而不会出现此错误。 rep 是一个 cython 内存视图,所以我不知道什么是 python 对象,可能是“返回”所指的是什么?
如果这没有封闭的解决方案,那么如何进行?
您的代码有一长串错误。当通常最好从第一个开始时,您会显示“最后一个”错误消息。不过,大多数都是相当琐碎的,所以我会跳过它们,只展示不明显的部分。 主要的是:
i:int
# and
j:int
当
int
用作
类型注释时,Cython 假定为 Python 整数。这与
cdef
声明不同。原因是
类型注释不一定针对 Cython,因此 Cython 应谨慎使用它们,不要改变代码的行为。
int
i: cython.int
。
“没有 gil”错误是因为您尝试使用 Python 对象索引rep
。