Python: 3.7
Cloud platform: Azure
Resource: Azure function
Goal: Improve the speed of the function by using global variables
Hi everyone,
I developed an azure function with python. I found the following definition in order to use global variables:
CACHED_DATA = None
def main(req):
global CACHED_DATA
if CACHED_DATA is None:
CACHED_DATA = load_json()
# ... use CACHED_DATA in code
Source: Python reference
I did not understand well what should add in my code. For that reason, I have the following questions:
Should the name of the variable always be named as "CACHE_DATA"?
If I set CACHED_DATA= None in my code, my understanding is every time that I call the function, this variable will be reset?.
Part of my current code is as following. Am I implementing correct that definition? If not, could someone tell how should I do it?
stop_words = None vocabulary = None vectorizer_parameters = None def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse: global stop_words global vocabulary global vectorizer_parameters if stop_words is None: stop_words_file_path = os_path.join(ROOT_DIR,'azure_function_app_sortierer','parameters','CustomStopWords.csv') df_stop_words = read_csv(stop_words_file_path) stop_words =df_stop_words['Stopwords'].tolist() if vocabulary is None or vectorizer_parameters is None: vocabulary = {} vectorizer_parameters = {} for v in ['clean_noCompound-tfidf_stopWords_unigrams', 'clean_noCompound-tfidf_stopWords_bigrams']: vocabulary_file_path = os_path.join(ROOT_DIR, 'azure_function_app_sortierer', 'model' , '00_' + v + '_Vocabulary.pkl') vocabulary[v] = joblib.load(vocabulary_file_path) vectorizer_parameters[v] = cfp.set_vectorizer_parameters(vectorizer_name=v,stopWords=stop_words,vocabulary=vocabulary[v])

