Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
335 stars 103 forks source link

Does Azure Function Python V2 supports multiple functions in a single Function Apps #1395

Closed tswh15 closed 8 months ago

tswh15 commented 8 months ago

Hi all,

I need help setting up a project structure for my scenario - 3 different functions deployed in a single Azure Functions App. As far as I know, the only way to do so is to put all code logic of 3 functions as well as triggers in a single function_app.py, which is impractical from code maintainability and scalability perspectives. Something like the one below will work, but I don't think I'd go with it.

app = func.FunctionApp()

@app.schedule(schedule="0 0 2 * * *", arg_name="myTimer", run_on_startup=True,
              use_monitor=False) 
def FunctionA(myTimer: func.TimerRequest) -> None:
     do_something_for_functionA()
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

@app.schedule(schedule="0 0 2 * * *", arg_name="myTimer", run_on_startup=True,
              use_monitor=False) 
def FunctionB(myTimer: func.TimerRequest) -> None:
    do_something_for_functionB()
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

@app.schedule(schedule="0 0 2 * * *", arg_name="myTimer", run_on_startup=True,
              use_monitor=False) 
def FunctionC(myTimer: func.TimerRequest) -> None:
    do_something_for_functionC()
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

I need something more management and organized like below one (if possible):

.
└── Functions/
    ├── .venv
    ├── .python_packages
    ├── .vscode
    ├── .funcignore
    ├── .gitignore
    ├── function_app.py
    ├── function_a.py
    ├── function_b.py
    ├── function_c.py
    ├── host.json
    ├── local.settings.json
    └── requirements.txt
gavin-aguiar commented 8 months ago

@tswh15 You can use blueprints to separate out functions in different files. You would only need to register them in the function_app.py file Here is more information on how to use blueprints. https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=asgi%2Capplication-level&pivots=python-mode-decorators#blueprints