What would be the best approach to run scheduled python script on azure which is making some calculation and storing result as CSV in Azure Blobs
2 Answers
The best solution to this problem is to create an Azure function with a timer trigger. You can configure the timer to execute as often as you want.
Here's an example for the timer configuration, which will execute the script timerfunc.py every working day at 12PM:
{
"scriptFile": "timerfunc.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 0 12 * * 1-5"
}
]
}
an example for timerfunc.py:
import datetime
import logging
import azure.functions as func
def main(mytimer: func.TimerRequest) -> None:
logging.info('Function executed!')
You can find more examples of Python code, as well as information on how to configure the timer, at the following documentation page https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=python
Comments
Update: The Python v2 programming model lets you define bindings using decorators directly in your Python function code.
Same example as above, the script will be triggered every working day at 12PM:
import logging
import azure.functions as func
app = func.FunctionApp()
@app.schedule(schedule="0 0 12 * * 1-5", arg_name="myTimer", run_on_startup=True,
use_monitor=False)
def test_my_timer(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function executed.')