Skip to content

Instantly share code, notes, and snippets.

@AlexTalker
Created March 5, 2020 09:49
Show Gist options
  • Save AlexTalker/e3564727e4bbb8f81ec0f9c54134ecd9 to your computer and use it in GitHub Desktop.
Save AlexTalker/e3564727e4bbb8f81ec0f9c54134ecd9 to your computer and use it in GitHub Desktop.
Direct I/O in Python with Cython
from libc.stdlib cimport malloc, free
from libc.stdio cimport *
from posix.unistd cimport *
from posix.stdlib cimport *
from posix.fcntl cimport *
def test_cython(path):
cdef int ret
cdef int stdout_save, dev_null
cdef int O_DIRECT = 040000
cdef char *buffer = <char *> malloc(512 * sizeof(char))
cdef char **bb = &buffer
posix_memalign(<void **>bb, 512, 512)
# buffer = *bb
dev_null = open(path.encode('utf8'), O_DIRECT | O_RDONLY, 0)
print(lseek(dev_null, 1024*1024+4096, 0))
print(read(dev_null, <void *> buffer, 512))
print(buffer)
ret = close(dev_null)
print(ret)
free(buffer)
# print("Hello World")
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("helloworld.pyx")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment