You can download this code by clicking the button below.
This code is now available for download.
This code defines a Celery task that simulates sending an email. It uses Celery's logger to log information and returns a message when the task is executed.
Technology Stack : Celery, Python
Code Type : Celery task
Code Difficulty : Intermediate
from celery import Celery
from celery.schedules import crontab
def task1(name, age):
"""
A Celery task that sends an email to a user.
"""
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
# Simulating sending an email
logger.info(f"Sending email to {name}, {age} years old.")
return f"Email sent to {name}"
# Configuration for Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//')
@app.task
def task1(name, age):
return f"Email sent to {name}"
# Celery beat schedule configuration
app.conf.beat_schedule = {
'send_email_every_day': {
'task': 'tasks.task1',
'schedule': crontab(hour=0, minute=0),
'args': ('John Doe', 30),
}
}