如何在Python中调用syscall readahead?

时间:2016-07-18 10:05:08

标签: file python-3.x file-io system-calls

如何在Python 3中调用readahead系统调用?

  

readahead()在文件上启动readahead,以便后续读取          从该文件将从缓存中满足,而不是阻止          磁盘I / O

1 个答案:

答案 0 :(得分:5)

使用ctypes并从libc输入系统调用:

    import ctypes, os

    # load ourselves, we already have libc
    libc = ctypes.CDLL(None, use_errno=True)

    # XXX - YMMV, ctypes doesn't have c_off_t much less c_off64_t.
    # Assume it's c_longlong, but don't count on that.
    off64_t = ctypes.c_longlong

    def readahead(fobj, offset, count):
        fno = fobj if isinstance(fobj, int) else fobj.fileno()

        code = libc.readahead(
            ctypes.c_int(fno),
            off64_t(offset),
            ctypes.c_size_t(count)
        )
        if code != 0:
            errno = ctypes.get_errno()
            raise OSError(errno, os.strerror(errno))