Skip to content

Instantly share code, notes, and snippets.

@nirs
Created December 19, 2017 00:57
Show Gist options
  • Save nirs/c44882ab64f48926b54eecf02eea6efa to your computer and use it in GitHub Desktop.
Save nirs/c44882ab64f48926b54eecf02eea6efa to your computer and use it in GitHub Desktop.
Copying data from disk to disk using direct io
import mmap
import io
from contextlib import closing
BUF_SIZE = 8 * 1024**2
buf = mmap.mmap(-1, BUF_SIZE)
with closing(buf), io.FileIO(src_path, "r") as src, io.FileIO(dst_path, "r+") as dst:
while True:
read = src.readinto(buf)
if read == 0:
break # done
written = 0
while written < read:
wbuf = buffer(buf, written, read - written)
written += dst.write(wbuf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment