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

6

我怎样在Python 3中调用readahead系统调用?

readahead()函数可在文件上启动预读,以便随后从该文件进行的读取将从缓存中满足,而不会阻塞磁盘I/O。

1个回答

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))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接