I have a config file that needs reloading occasionally. The reload is called inside a method of a class that's running within a thread.
I have tried to do this by using globals()
but despite the fact print globals()
show that they have updated the program does not see the change.
I tried to add global variableName
everywhere before using them but still nothing.
After that I considered using __main__
as i know that works but my problem is I cannot set __main__
variables when my variable names are strings read from a file as are the values.
This is my config parser:
def parse_config(path):
# grab config.ini
ini_file = open(root_path + path)
ini_contents = ini_file.read().replace('\r\n', '\n')
ini_file.close()
ini_list = filter(None, ini_contents.split("\n"))
ini_settings = [x for x in ini_list if not x.startswith('#')]
# loop config file lines and set variables
for line in ini_settings:
splits = line.split('=', 1)
name = splits[0].strip()
value = splits[1].strip()
value = value.replace('THIS/', root_path)
if value[0] == ':':
value = value[1:].split(',')
elif str(value).lower() == 'true':
value = True
elif str(value).lower() == 'false':
value = False
globals().update({name:value})
Example ini file
# Path to folder where error logs get written
error_log_path = THIS/logs/
# Print in console (for debugging only)
logg_print = True
# Log to file
logg_write = True
logg_file_path = THIS/logs/
Main app example
class NotifyHandler():
def __init__(self):
m = threading.Thread(target=self.process_send)
m.setDaemon(True)
m.start()
def process_send(self):
while True:
parse_config('config.ini')
print logg_print
time.sleep(10)
NotifyHandler()
while True:
time.sleep(10)