Celery Task for Summing Integers

  • Share this:

Code introduction


This function is a Celery task that accepts three integer arguments, calculates their sum, and uses Celery's logging feature to record the result.


Technology Stack : Celery, Python's logging module

Code Type : Celery mission

Code Difficulty : Intermediate


                
                    
def random_task(arg1, arg2, arg3):
    from celery import Celery
    from celery.utils.log import get_task_logger

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

    if not isinstance(arg1, int) or not isinstance(arg2, int) or not isinstance(arg3, int):
        logger.error("All arguments must be integers")
        return "Error: All arguments must be integers"

    sum_result = arg1 + arg2 + arg3
    logger.info(f"The sum of {arg1}, {arg2}, and {arg3} is {sum_result}")
    return sum_result