I create a dictionary from a remote database as part of my application run. This process is pretty I/O heavy, so I've decided to create a "singleton" instance of this dictionary and just call it as it is needed in my application.
The code looks like (in Dictionaries.py
):
state_code_dict = None
def get_state_code_dict():
global state_code_dict
if state_code_dict == None:
state_code_dict = generate_state_code_dict()
return state_code_dict
I then import and call the get_state_code_dict()
function where needed. I added a print statement to check if state_code_dict
was being reinitialized or reused, and I found it was being reused (which is the functionality I want). Why is the instance of state_code_dict
surviving the application run?
Edit
I import the get_state_code_dict
function in multiple files.