You can download this code by clicking the button below.
This code is now available for download.
This code defines a Celery task that generates a random number and prints the result. It first creates a Celery instance, then defines a task `generate_random_number` that uses `random.randint` to generate a random number between 1 and 100. The task is then executed asynchronously using `delay()`, and the result is retrieved and printed using `get()`.
Technology Stack : Celery, random
Code Type : The type of code
Code Difficulty : Intermediate
from celery import Celery
import random
def random_task():
# Create a Celery instance
app = Celery('tasks', broker='pyamqp://guest@localhost//')
# Define a task that will be used to generate a random number
@app.task
def generate_random_number():
return random.randint(1, 100)
# Call the task and print the result
result = generate_random_number.delay()
print(result.get(timeout=10))