django+celery的初步使用

# 使用celery
from django.core.mail import send_mail
from django.conf import settings
from celery import Celery
import time

# 在任务处理者worker一端加这几句
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mydailyfresh.settings")
django.setup()

# 创建一个Celery类的实例对象
app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/8')

# 定义任务函数
@app.task
def send_register_active_email(to_email, username, token):
    '''发送激活邮件'''
    # 组织邮件信息
    subject = '天天生鲜欢迎信息'
    message = ''
    sender = settings.EMAIL_FROM
    receiver = [to_email]
    html_message = '

%s, 欢迎您成为天天生鲜注册会员

请点击下面链接激活您的账户
http://127.0.0.1:8000/user/active/%s' % (username, token, token) send_mail(subject, message, sender, receiver, html_message=html_message) time.sleep(5)

 

linux:

celery -A celery_tasks.tasks worker -l info

windows:

celery -A celery_tasks.tasks worker --loglevel=info --pool=solo

调用:

send_register_active_email.delay(email, username, token)

 

 

 

你可能感兴趣的:(django,celery,中间件,django)