Celery Asynchronous Task Generating Random Number

  • Share this:

Code introduction


This function is an asynchronous task in Celery that generates a random number and returns it, while also logging information.


Technology Stack : Celery, Python logging module

Code Type : Asynchronous tasks

Code Difficulty : Intermediate


                
                    
def random_task(arg1, arg2):
    from celery.utils.log import get_task_logger
    import random
    from celery import current_app

    logger = get_task_logger(__name__)
    logger.info(f"Received arguments: {arg1}, {arg2}")

    # Simulate a time-consuming task
    result = random.random()
    logger.info(f"Task result: {result}")

    # Return the result
    return result