You can download this code by clicking the button below.
This code is now available for download.
This function uses the Celery library to create two simple tasks: add and multiply, and then randomly selects one task to execute, returning the result.
Technology Stack : Celery, RabbitMQ
Code Type : Celery mission
Code Difficulty : Intermediate
def random_select_task(arg1, arg2):
from celery import Celery
from celery.utils.log import get_task_logger
from random import choice
app = Celery('tasks', broker='pyamqp://guest@localhost//')
logger = get_task_logger(__name__)
@app.task
def add(x, y):
return x + y
@app.task
def multiply(x, y):
return x * y
task_type = choice(['add', 'multiply'])
result = None
if task_type == 'add':
result = add.delay(arg1, arg2)
elif task_type == 'multiply':
result = multiply.delay(arg1, arg2)
return result.get()