Celery Task Queue Addition Function

  • Share this:

Code introduction


This function adds a task to the Celery task queue. It accepts the task name, positional arguments, and keyword arguments, then sends the task to the queue using the `task.delay()` method. It also logs an informational message.


Technology Stack : Celery, Standard Python Library

Code Type : Celery task queue

Code Difficulty : Intermediate


                
                    
def add_task_to_queue(task_name, *args, **kwargs):
    from celery import Celery
    from celery.utils.log import get_task_logger

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

    task = app.tasks[task_name]
    task.delay(*args, **kwargs)
    logger.info(f'Task {task_name} added to queue with args {args} and kwargs {kwargs}')