Celery-based Data Processing with Random Task Simulation

  • Share this:

Code introduction


The code defines a function named random_task that accepts a task name and data as arguments. It uses the Celery framework to create an asynchronous task that processes the data (simply doubling it in this case). The function returns the processed result.


Technology Stack : Celery

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
from celery import Celery
import random

def random_task(name, data):
    app = Celery('tasks', broker='pyamqp://guest@localhost//')
    
    @app.task
    def process_data(data):
        # Simulating some processing
        return data * 2
    
    result = process_data.delay(data)
    return result.get(timeout=10)                
              
Tags: