gpt4 book ai didi

python - Cython:如何在没有 GIL 的情况下进行打印

转载 作者:太空狗 更新时间:2023-10-29 22:03:59 24 4
gpt4 key购买 nike

我应该如何在没有 gil 的 Cython 函数中使用 print?例如:

from libc.math cimport log, fabs
cpdef double f(double a, double b) nogil:
cdef double c = log( fabs(a - b) )
print c
return c

编译时报错:

Error compiling Cython file:
...
print c
^
------------------------------------------------------------

Python print statement not allowed without gil
...

我知道如何使用 C 库而不是它们的 python 等价物(例如此处的 math 库),但我找不到用于 print 的类似方法。

最佳答案

使用 stdio 中的 printf:

from libc.stdio cimport printf
...
printf("%f\n", c)

关于python - Cython:如何在没有 GIL 的情况下进行打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48363425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com