Celery Task for Adding Numbers with Logging

  • Share this:

Code introduction


This function creates a Celery task that adds two numbers and logs the result.


Technology Stack : Celery, Python

Code Type : Celery mission

Code Difficulty : Intermediate


                
                    
def random_celery_function():
    import random
    from celery import Celery
    from celery.utils.log import get_task_logger

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

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

    result = add.delay(4, 4)
    logger.info(f'The result is {result.get()}')

    return result.get()                
              
Tags: