Hello @JDR
Yes, switching to an async Azure Function would be a better solution for running code in a fire-and-forget manner. In an async Azure Function, you can use the asyncio.create_task()
function to create a task and run it in the background. Here's an example:
import asyncio
import azure.functions as func
async def main(req: func.HttpRequest) -> func.HttpResponse:
# Your code here
# Create a task and run it in the background
asyncio.create_task(my_background_task())
# Return a response
return func.HttpResponse("Task started")
async def my_background_task():
# Your background task code here
await asyncio.sleep(10)
print("Background task completed")
In this example, the my_background_task()
function is run in the background using asyncio.create_task()
. The main()
function returns a response immediately after starting the background task, without waiting for it to complete. This allows the function to continue processing other requests while the background task runs. Note that you should avoid using loop.run_in_executor()
in an Azure Function, as it can block the event loop and reduce the function's performance. Instead, use asyncio.create_task()
or other asyncio functions to run code asynchronously.
I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.