How to create global variables in azure function with python

Viewed 116

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:

  1. Should the name of the variable always be named as "CACHE_DATA"?

  2. If I set CACHED_DATA= None in my code, my understanding is every time that I call the function, this variable will be reset?.

  3. 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])
    
1 Answers
  • Global keyword is used to create and modify global variables in python.

  • Global is used to either alter or create global variables inside a function. eg:

    test_variable = "Hi I am azure "
    def test_function():
            print(test_variable)
            global test_variable = "Hi I am Microsoft"
            print(test_variable)
    

    Here test_variable is the global variable and it is altered in the function. So, using Global outside a function is useless

  • While in case to global variables in azure function are concerned they will not be reset every time you call an azure function.

  • Consider the following scenario where I set a global variable which is a string that says "Hi I am Azure " after deployment to azure after multiple request the end of the response always end with global variable.

enter image description here

enter image description here

Notice for different get request with different parameter the end of the response is always “Hi I am Azure”.

Related