【问题标题】:How to call syscall readahead in Python?如何在 Python 中调用系统调用预读?
【发布时间】:2016-11-20 21:29:19
【问题描述】:

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

readahead() 对文件启动预读,以便后续读取 来自该文件的内容将从缓存中得到满足,而不是阻塞 磁盘 I/O

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 2011-10-24
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      相关资源
      最近更新 更多