我尝试编译以下Cython代码,它使用C函数进行文件操作:
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()
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
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
Run Code Online (Sandbox Code Playgroud)
我认为只有python功能是需要gil而不是导入功能C.然而,似乎并非如此.
因此,我的问题是:
Cython没有GIL?GIL?您正在隐藏声明libc.stdio的声明
cdef extern from "stdio.h" nogil:
Run Code Online (Sandbox Code Playgroud)
你自己的定义没有nogil.要回答标题中的问题:只有Python/C API函数需要gil.
这是您的代码具有正确的导入和修剪任何不相关的东西:
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()
Run Code Online (Sandbox Code Playgroud)
以下是确保延长返回的临时生命周期的必要条件tmpfile.name.encode.
cdef bytes py_fname = tmpfile.name.encode("UTF-8")
cdef char* fname = py_fname
Run Code Online (Sandbox Code Playgroud)
使用时编译时不会出错
cython -3 --cplus my_mod.pyx
g++ my_mod.cpp -shared -o my_mod.so $(python3.4 --cflags --ldflags)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1472 次 |
| 最近记录: |