1

I have a function app with about 10 functions in it.

I try to set a variable at the global scope to load the file. (I suppose that after deployment, all the code in global scope should be executed once. So the file only has to be loaded once, no matter how many times of function invocation)

Here is my question: Some of the functions have to load the same file for calculation. Since all functions run in the same function app, is it possible to share variables between functions? So that I can reduce memory usage.

Any suggestions?

Tamil Selvan
  • 1,600
  • 1
  • 9
  • 25
han shih
  • 389
  • 1
  • 5
  • 13

2 Answers2

1

Thanks @ thiago-custodio for your answer. Each function app runs in its own process and all functions run in that process. Static variables are shared across all functions in that application. For example, you can create an HttpClient in a static class and all functions can access that same client instance (we encourage this, in fact). It's always been this way, so nothing changed there.

The issue still available please check the github issue

Refer here

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15
  • Thanks for your reply. I have read those links before asking this question. As far as I know, there is no "static variable" in Python. So I am wondering if it is possible to achieve this in Python. – han shih Jan 03 '22 at 09:05
  • @hanshih it is. Please see the answer below. – Daniel Kusy Jun 08 '22 at 21:47
1

For anyone who wonder if this is possible in Python since there are no "real" static variables, yes it is. And it's simpler than you think. All you need to do is to import the variable from another's function module and that's it.

First function:

# Foo
import logging
import azure.functions as func
from src.foo import total

def main(req: func.HttpRequest):
    logging.info(id(total))
    return func.HttpResponse()

and the second one:

# Bar
import logging
import azure.functions as func
    
total = 10000

def main(req: func.HttpRequest):
    logging.info(id(total))
    return func.HttpResponse()

The output from running the app with func start:

Functions:

        foo: [GET,POST] http://localhost:7071/foo

        bar: [GET,POST] http://localhost:7071/bar

For detailed output, run func with --verbose flag.
[2022-06-08T21:43:58.463Z] Worker process started and initialized.
[2022-06-08T21:43:59.297Z] Executing 'Functions.foo' (Reason='This function was programmatically called via the host APIs.', Id=d8ef25ff-c913-4351-a3fd-468a252af9e3)
[2022-06-08T21:43:59.353Z] 9792864
[2022-06-08T21:43:59.421Z] Executed 'Functions.foo' (Succeeded, Id=d8ef25ff-c913-4351-a3fd-468a252af9e3, Duration=149ms)
[2022-06-08T21:44:02.633Z] Executing 'Functions.bar' (Reason='This function was programmatically called via the host APIs.', Id=3b2c66a2-7127-4ddd-9ce8-6f8f7c62a4cf)
[2022-06-08T21:44:02.642Z] 9792864
[2022-06-08T21:44:02.644Z] Executed 'Functions.bar' (Succeeded, Id=3b2c66a2-7127-4ddd-9ce8-6f8f7c62a4cf, Duration=14ms)
[2022-06-08T21:44:03.133Z] Host lock lease acquired by instance ID '000000000000000000000000AD81EAAA'.

As you can see, the IDS are the same.

Daniel Kusy
  • 322
  • 1
  • 3
  • 16