【发布时间】:2020-04-27 05:31:31
【问题描述】:
我正在尝试从这样定义的 C++ 库中调用一个函数:
int somefunc(float timeout);
我将pxd 文件中的定义翻了一番:
int somefunc(float timeout) nogil;
但是在对pyx 文件进行 cythonizing 时,任何使用 nogil 调用函数的尝试都会导致相同的错误:
timeout = 1.0
with nogil:
Q.somefunc(timeout)
Error compiling Cython file:
------------------------------------------------------------
...
int(block), timeout,
)
timeout = 1.0
with nogil:
Q.somefunc(timeout)
^
------------------------------------------------------------
script.pyx:105:23: Coercion from Python not allowed without the GIL
我也尝试使用 ctype 调用它,这会导致同样的错误。
timeout = ctypes.c_float(1.0)
with nogil:
Q.somefunc(timeout)
仅使用浮点文字有效。使用实际 Python 变量调用此函数的正确方法是什么?
【问题讨论】: