How can I call the readahead syscall in Python 3?
readahead() initiates readahead on a file so that subsequent reads from that file will be satisfied from the cache, and not block on disk I/O
Use ctypes
and pull in the system call from 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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With