I'm trying to use mmap to load a dictionary from file. I'll explain my problem on simplified example. In real, I have 10 files, which have to be loaded in miliseconds (or act like been loaded).
So lets have a dictionary - 50 mb. My program should find a value by key under 1 sec. Searching in this dictionary is not a problem, it could be done far under 1 sec. The problem is that when sb puts an input into the text field and press enter, the program starts to load the dictionary into memory so program can find a key. This loading can takes several seconds but I have to get result under 1 sec (dictionary can't be loaded before pressing enter). So I was recommended to use mmap module which should be far faster.
I can't google a good example. I've tried this (I know that it is an incorrect use)
def loadDict():
with open('dict','r+b') as f: # used pickle to save
fmap = mmap.mmap(f.fileno(),0)
dictionary = cpickle.load(fmap)
return dictionary
def search(pattern):
dictionary = loadDict()
return dictionary['pattern']
search('apple') <- it still takes many seconds
Could you give me a good example of proper mmap use?
mmapis to map a file into memory and implement demand paging. This means a particular segment will only be read from disk into memory the first time you access it (but then stays in memory). That means that repeatedly accessing the same chunks of a file and seeking forth and back in the file will be very fast. But since for your purpose you basically need random access to the entire file, usingmmapis obviously not going to help here, but instead make things worse.