'Accessing result of Django Celery tasks whenever they finish

When a user clicks a button on my webpage 2 celery tasks are started (using RabbitMQ):

views.py

def btn_clicked(request):
    task1 = task1.delay() 
    task2 = task2.delay()

tasks.py

@shared_task
def task1:
    #do something
    return result1

@shared_task
def task2:
    #do something
    return result2

I want to use htmx to update the html on my webpage when the results of task1 and task2 are available (as they come in, task1 may be quicker than task2). The tasks will take ~30 seconds.

In order to pass the info to the html I need to access the result as soon as it's available but I'm not sure how. From what I've read elsewhere it may be through using AsyncResult but I'm not sure on this. I don't want to store the result anywhere, I'll immediately pass it to my webpage.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source