Cython不要求gil哪些C函数? [英] Which C functions are not gil-requiring in Cython?
问题描述
我尝试编译以下 Cython
代码,该代码使用 C
函数进行文件操作:
I try to compile the following Cython
code, which uses C
functions for file operations:
import tempfile
from libc.stdio cimport *
cdef extern from "stdio.h":
FILE *fopen(const char *, const char *)
int fclose(FILE *)
size_t fwrite(const void *, size_t, size_t, FILE *)
ssize_t getline(char **, size_t *, FILE *)
def run_io():
cdef int ntasks
cdef int i
cdef string dump = "Some string"
cdef string content = ""
cdef char* fname
cdef FILE* cfile
cdef char* line = NULL
cdef size_t l = 0
tmpfile = tempfile.NamedTemporaryFile('w+')
fname = tmpfile.name.encode("UTF-8")
with nogil:
cfile = fopen(fname, "wb")
#fwrite(dump.data(), 1, dump.size(), cfile)
#fclose(cfile)
#cfile = fopen(fname, "rb")
#if getline(&line, &l, cfile) == -1:
#break
#else:
#printf("%s", line)
fclose(cfile)
tmpfile.close()
但是,出现以下错误:
Error compiling Cython file:
------------------------------------------------------------
...
#cfile = fopen(fname, "rb")
#if getline(&line, &l, cfile) == -1:
#break
#else:
#printf("%s", line)
fclose(cfile)
^
------------------------------------------------------------
test.pyx:31:14: Calling gil-requiring function not allowed without gil
,我不允许调用gil-requireing函数我以为只有 python
函数需要gil,但不能导入 C
函数。
I thought that only python
functions are gil-requiring but not imported C
ones. Nevertheless, it seems like it is not so.
因此,我的问题是:
- 哪些C函数可以在
Cython
中使用而无需GIL
? - 如何在不使用
GIL
的情况下进行文件读写?
- Which C functions can be used in
Cython
withoutGIL
? - How to make file read/write without
GIL
?
推荐答案
您正在隐藏 libc.stdio
中的声明,这些声明是用
You are shadowing the declarations from libc.stdio
which are declared with
cdef extern from "stdio.h" nogil:
具有您自己的定义其中没有 nogil
。要回答标题中的问题:只有python / C API函数需要gil。
with your own definitions which do not have nogil
. To answer the question in your title: only Python/C API functions require the gil.
这是您的代码,具有正确的导入和不相关的内容:
This is your code with correct imports and trimmed of anything not relevant:
import tempfile
from libc.stdio cimport fopen, fclose, fwrite, getline, FILE
from libcpp.string cimport string
def run_io():
cdef string dump = b"Some string"
tmpfile = tempfile.NamedTemporaryFile('w+')
cdef bytes py_fname = tmpfile.name.encode("UTF-8")
cdef char* fname = py_fname
cdef FILE* cfile
with nogil:
cfile = fopen(fname, "wb")
fclose(cfile)
tmpfile.close()
以下是必要的以确保延长 tmpfile.name.encode
返回的临时的生存期。
The following is necessary to ensure that the lifetime of the temporary returned by tmpfile.name.encode
is extended.
cdef bytes py_fname = tmpfile.name.encode("UTF-8")
cdef char* fname = py_fname
使用$ p $
It gives no errors when compiled with
cython -3 --cplus my_mod.pyx
g++ my_mod.cpp -shared -o my_mod.so $(python3.4 --cflags --ldflags)
这篇关于Cython不要求gil哪些C函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!