在Cython中哪些C函数不需要gil?

Rom*_*man 0 python cython

我尝试编译以下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.然而,似乎并非如此.

因此,我的问题是:

  1. 其中C函数可用于Cython没有GIL
  2. 如何在没有文件读/写的情况下GIL

jep*_*pio 7

您正在隐藏声明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)