Skip to content

Instantly share code, notes, and snippets.

@tianweidut
Created November 11, 2014 04:20
Show Gist options
  • Save tianweidut/ab9fa6643b12f7c1366b to your computer and use it in GitHub Desktop.
Save tianweidut/ab9fa6643b12f7c1366b to your computer and use it in GitHub Desktop.
mmap
#coding: utf-8
import sys
import os
import mmap
def get_mmap(filepath):
with open(filepath, 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
return mm
def test(path):
mm = get_mmap(path)
a = raw_input('input:')
print mm.readline()
mm[:3] = 'new'
mm.seek(0)
print mm.readline()
a = raw_input('input:')
mm.close()
if __name__ == "__main__":
path='/tmp/test_mmap.txt'
with open(path, 'w') as f:
f.writelines('test test.....')
test(path)
#coding: utf-8
import sys
import os
import mmap
def get_mmap(filepath):
with open(filepath, 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
return mm
def test(path):
mm = get_mmap(path)
print mm.readline()
a = raw_input('wait1:')
mm.seek(0)
print mm.readline()
mm.close()
if __name__ == "__main__":
path='/tmp/test_mmap.txt'
test(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment