Prompt
How do I run Python background jobs on Azure Functions?
Latest observation
To run Python background jobs on Azure Functions, you typically use timer-triggered functions or queue-triggered functions, depending on what you mean by “background jobs.”
Common options
1) Timer Trigger
Use this when you want code to run on a schedule, like every 5 minutes or nightly.
Example (function_app.py for Python v2 model):
import logging
import azure.functions as func
app = func.FunctionApp()
@app.schedule(schedule="0 */5 * * * *", arg_name="mytimer", run_on_startup=False, use_monitor=True)
def scheduled_job(mytimer: func.TimerRequest):
logging.info("Python background job ran on a schedule")
Use cases:
- periodic cleanup
- syncing data
- sending reminders
- polling an API
2) Queue Trigger
Use this when you want a background job to run when a message is added to a queue.
Example:
import logging
import azure.functions as func
app = func.FunctionApp()
@app.queue_trigger(arg_name="msg", queue_name="jobs", connection="AzureWebJobsStorage")
def process_job(msg: func.QueueMessage):
logging.info(f"Processing job: {msg.get_body().decode('utf-8')}")
Use cases:
- async work after a web request
- decoupled task processing
- retries on failure
3) HTTP-triggered function that enqueues work
If you want a web request to start a background task, don’t do the heavy work directly in the HTTP request. Instead:
- Receive the request
- Put a message on a queue
- Let a queue-triggered function do the job
This is the standard pattern for long-running work.
Important notes for Python on Azure Functions
Don’t rely on in-memory background threads
Azure Functions can scale out or recycle instances, so starting your own threads or long-running background loops inside a function app is unreliable.
Avoid patterns like:
- infinite loops in startup code
- daemon threads that keep running after the request ends
- local in-memory job queues
Use Azure-managed triggers instead.
If your job is long-running
Azure Functions has execution limits depending on plan:
- Consumption plan: limited execution time
- Premium/Dedicated: better for longer jobs
For very long workflows, consider:
- Durable Functions
- Azure Container Apps Jobs
- Azure WebJobs
- Azure Batch if it’s compute-heavy
Quick setup steps
- Install Azure Functions Core Tools
- Create a Python Functions app
- Add a timer or queue trigger
- Deploy to Azure
- Monitor logs in Application Insights / Log Stream
Best practice recommendation
If you mean “run work in the background after a user request,” use:
- HTTP trigger → enqueue message
- Queue trigger → do the actual job
If you mean “run on a schedule,” use:
- Timer trigger
If you want, I can give you:
- a full Python Azure Functions project example, or
- a step-by-step guide to deploy one to Azure.