I am creating dictionary out of a large file.
def make_dic():
big_dic={}
for foo in open(bar):
key,value=do_something(foo)
big_dic[key]=value
def main():
make_dic() #this takes time
I have to access this dictionary many times but from completely different programs. It takes lot of time to read this file and make dictionary. Is it possible to make a dictionary which remains in memory even if one program exits???? So that I create it once but can use it again and again from different programs….
This won’t work for all situations that fit your description, but
cPickleshould help with speed.The only problem I can think of is that combining data persistence with IPC is tough. So if these different programs are modifying the dictionary at the same time,
picklewon’t help. Another approach might be to use a database…I like Sven Marnach‘s suggestion, but there are some tradeoffs worth considering. Some setup…
Obviously populating the
anydbm_filewill be pretty slow:The time is comparable to the time it takes to dump and load a pickle file:
But the
anydbm_fileyou only have to create once; then, opening it again is nigh-instantaneous.So
anydbmhas the advantage there. On the other hand,Reading a key from
anydbm_filetakes ten times longer than reading a key from a dictionary in memory. You’d have to do a lot of lookups for this difference to outweigh the 5 seconds necessary for a pickle dump/load cycle; but even if you don’t, the difference in read times here could lead to sluggish performance, depending on what you’re doing.Other options are
SQLite3or (for a separate database server process that allows connections from multiple processes running concurrently),psycopg2+ PostgreSQL.