可以在循环中多次使用 getline() 吗?- Cython,文件读取

Syl*_*ain 7 python stdio getline cython file-read

我想读取一个 4 行 4 行的文件(它是一个带有 DNA 序列的 fastq 文件)。
当我一行一行或一行一行地读取文件时,没有问题,但是当我一次读取 3 或 4 行时,我的代码崩溃了(内核似乎在 jupyter notebook 上死了)。(取消注释最后一部分,或 4 中的任何 3 部分getline()
我尝试使用 char (char**) 的双数组来存储行,但存在相同的问题。

知道是什么原因吗?

使用 Python 3.7.3、Cython 0.29,更新了所有其他库。正在读取的文件大约为 1.3GB,机器有 8GB,ubuntu 16.04。代码改编自https://gist.github.com/pydemo/0b85bd5d1c017f6873422e02aeb9618a

%%cython
from libc.stdio cimport FILE, fopen, fclose, getline
    
def fastq_reader(early_stop=10):
    cdef const char* fname = b'/path/to/file'
    cdef FILE* cfile
    cfile = fopen(fname, "rb")

    cdef:
        char * line_0 = NULL
        char * line_1 = NULL
        char * line_2 = NULL
        char * line_3 = NULL
        size_t seed = 0
        ssize_t length_line
        unsigned long long line_nb = 0

    while True:
        length_line = getline(&line_0, &seed, cfile)
        if length_line < 0: break
        
        length_line = getline(&line_1, &seed, cfile)
        if length_line < 0: break
        
#         length_line = getline(&line_2, &seed, cfile)
#         if length_line < 0: break
        
#         length_line = getline(&line_3, &seed, cfile)
#         if length_line < 0: break

        line_nb += 4
        if line_nb > early_stop:
            break

    fclose(cfile)
    return line_nb

fastq_reader(early_stop=20000)
Run Code Online (Sandbox Code Playgroud)

Syl*_*ain 2

根本问题是我对getline() getline() c 参考的误解

n为了将行存储在不同的变量中,每个行指针都需要关联*lineptr

如果*lineptr在调用前设置为NULL并且*n设置为0,那么getline()将分配一个缓冲区来存储该行。

或者,在调用 getline() 之前,*lineptr 可以包含指向 malloc(3) 分配的缓冲区 *n 字节大小的指针。如果缓冲区不够大,无法容纳该行,则 getline() 使用 realloc(3) 调整其大小,并根据需要更新 *lineptr 和 *n。

nseed在我的代码中)将保存为指针分配的缓冲区的大小,其中 getline() 放置传入行。当我为不同的指针设置相同的缓冲区变量时,getline 得到了有关 char * line_xxx 大小的错误信息


由于 fastq 文件通常是这种形状:

@read_id_usually_short
CTATACCACCAAGGCTGGAAATTGTAAAACACACCGCCTGACATATCAATAAGGTGTCAAATTCCCTTTTCTCTAGCTTTCGTACT_very_long
+
-///.)/.-/)//-//..-*...-.&%&.--%#(++*/.//////,/*//+(.///..,%&-#&)..,)/.,.._same_length_as_line_2
Run Code Online (Sandbox Code Playgroud)

对于具有相同缓冲区长度的一两个 getline() 没有错误,因为缓冲区太小并且 getline 增大了指针的大小。
但是当使用 3 或 4 个 getlines() 时,调用被length_line = getline(&line_2, &seed, cfile)要求存储长度为 2 的 char* ('+\n'),同时得到(错误的信息)指针line_2已经足够大(line_1 的大小) 。


所以(简单的)解决方案是

%%cython
from libc.stdio cimport FILE, fopen, fclose, getline
    
def fastq_reader(early_stop=10):
    cdef const char* fname = b'/path/to/file'
    cdef FILE* cfile
    cfile = fopen(fname, "rb")

    cdef:
        char * line_0 = NULL
        char * line_1 = NULL
        char * line_2 = NULL
        char * line_3 = NULL
        # One variable for each line pointer
        size_t n_0 = 0
        size_t n_1 = 0
        size_t n_2 = 0
        size_t n_3 = 0
        ssize_t length_line
        unsigned long long line_nb = 0

    while True:
        # Reading the same file (same cfile), but line_x and n_x by pairs)
        length_line = getline(&line_0, &n_0, cfile)  
        if length_line < 0: break
        
        length_line = getline(&line_1, &n_1, cfile)
        if length_line < 0: break
        
        length_line = getline(&line_2, &n_2, cfile)
        if length_line < 0: break
        
        length_line = getline(&line_3, &n_3, cfile)
        if length_line < 0: break

        line_nb += 4
        if line_nb > early_stop:
            break

    fclose(cfile)
    return line_nb

fastq_reader(early_stop=20000)
Run Code Online (Sandbox Code Playgroud)

感谢您指出我的错误。