Celery Integration for Asynchronous Addition Task

  • Share this:

Code introduction


This function defines a function named random_task that accepts two parameters x and y. The function internally creates a Celery application and defines a task named add that takes two arguments and returns their sum. Then, it uses the add task to asynchronously add x and y, and returns the result.


Technology Stack : Celery, Python, RabbitMQ

Code Type : Celery mission

Code Difficulty : Intermediate


                
                    
def random_task(x, y):
    import random
    import celery
    from celery import Celery

    app = Celery('tasks', broker='pyamqp://guest@localhost//')

    @app.task
    def add(x, y):
        return x + y

    result = add.delay(x, y)
    return result.get(timeout=10)