Skip to content

Instantly share code, notes, and snippets.

@Happyholic1203
Created October 9, 2020 03:59
Show Gist options
  • Save Happyholic1203/fbe677431f2d73a42fcb43184731753e to your computer and use it in GitHub Desktop.
Save Happyholic1203/fbe677431f2d73a42fcb43184731753e to your computer and use it in GitHub Desktop.
Fast tail(1) implementation using mmap in Python
import os
import sys
import mmap
def tail(filepath, numlines):
with open(filepath, 'rb') as f:
mapped = mmap.mmap(f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
size = os.stat(filepath).st_size
try:
for i in range(size - 1, -1, -1):
if mapped[i] == '\n':
numlines -= 1
if numlines < 0:
break
return mapped[i + 1 if i > 0 else 0:]
finally:
mapped.close()
def main():
if len(sys.argv) != 3:
sys.stderr.write('Usage: {0} /path/to/file N\n'.format(sys.argv[0]))
return 1
sys.stdout.write(''.join(tail(sys.argv[1], int(sys.argv[2]))))
return 0
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment