I have a file called mapping.py which has a dictionary methodMapping. In my web application, a key-value pair is added to the methodMapping dictionary. After I appended it to mapping.py, reload(mapping) is called, and the file reloads (I checked by printing out a message on top of the file), but when I try to access the key-value pair, KeyError is raised.
original mapping.py file:
print('mapping.py loaded) methodMapping = {}
methodMapping['key1'] = 'value1'
here is how the key-value pair is appended to the file:
from mapping import methodMapping
@app.route('/append', methods=['POST'])
def append():
key = request.form.get('key')
value = request.form.get('value')
value = value.encode('ascii', 'ignore')
f = open('mapping.py', 'a')
f.write('methodMapping["'+key+'"] = '+value)
f.write("\n\n")
f.close()
reload(mapping)
return ....
after a key-value is added, mapping.py looks like this:
print('mapping.py loaded')
methodMapping = {}
methodMapping['key1'] = 'value1'
methodMapping['key2'] = 'value2'
however, when I try to access methodMapping['key2'] from flaskServer.py, KeyError exception is raised. When I restart the server, it is able to find methodMapping['key2'].
Note: I have already checked this link also tried app.run(debug=True, port=8000), but this is not possible for my application because I'm using keras with Tensorflow backend, and setting debug=True will load it twice and leads to a ValueError: Tensor tensor(...) is not part of the graph error
Any comment or suggestion is greatly appreciated. Thank you.