How to fire and forget a task in python within an azure function

JDR 90 Reputation points
2024-07-12T05:41:16.17+00:00

Hi,

My API is backed by an HTTP-triggered Azure Function. It processes data that it enqueues in batches into a storage queue.

It turns out that the enqueuing is fairly slow.

Is there a way in Python to run code within a synchronous Azure Function in a fire-and-forget manner (i.e., non-blocking)?

I tried the code below, and asyncio.get_event_loop() does return an event loop, but it is not running (is_running() is False).

Would switching to an async Azure Function (per Azure documentation) be the solution?

Thanks in advance for any help.

import asyncio

def fire_and_forget(task, *args, **kwargs):
	loop = asyncio.get_event_loop()        
	return loop.run_in_executor(None, task, *args, **kwargs)  

async def foo():     
	# Asynchronous stuff here  fire_and_forget(foo) 

Also tried:

asyncio.ensure_future(task(*args, **kwargs), loop=loop)

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,694 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pinaki Ghatak 3,265 Reputation points Microsoft Employee
    2024-07-12T09:43:34.7666667+00:00

    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.


0 additional answers

Sort by: Most helpful