BZ易风

导航

 

安装

pip install celery==4.2.0   # celery4.x支持django1.11以上版本 试了好几个版本 就4.2.0能发送成功

1.项目目录里新建一个celery的包celery_tasks

 tasks.py中

from django.conf import settings
from django.core.mail import send_mail

from celery import Celery  # 导入celery包# 创建一个Celery类的实例对象
# Celery(名称(一般写包名), 中间人broke)
app = Celery('celery_tasks.tasks', broker='redis://192.168.199.130:6379/2')  # 2 使用第二个数据库


# 定义任务函数
@app.task  # 装饰 必不可少
def send_register_active_email(to_email, username, token):
    # '''发送激活邮件'''
    # 组织邮件信息
    # 发送邮件
    subject = '天天生鲜欢迎信息'  # 邮件主题
    # 邮件正文
    # 注:此处html标签是不会被解析出来 会当作字符串输出
    # message = '<h1>%s, 欢迎您成为天天生鲜注册会员</h1>请点击下面链接激活您的账户:<br/><a href="http://192.168.199.130:8000/user/active/%s">http://192.168.199.130:8000/user/active/%s</a>' % (username, token, token)
    message = ''
    sender = settings.EMAIL_FROM  # 发件人
    receiver = [to_email]  # 收件人列表
    html_message = '<h1>%s, 欢迎您成为天天生鲜注册会员</h1>请点击下面链接激活您的账户<br/><a href="http://192.168.199.130:8000/user/active/%s">http://192.168.199.130:8000/user/active/%s</a>' % (
    username, token, token)
    print(html_message)
    # send_mail(邮件主题, 邮件正文, 发件人, 收件人列表, html_message=HTML格式的内容)
    # send_mail(subject, message, sender, receiver)
    send_mail(subject, message, sender, receiver, html_message=html_message)

2.在视图函数中导入tasks包就行

from celery_tasks.tasks import send_register_active_email  # 导入celery发邮件的方法

# 发送邮件
# delay(收件人, 用户名, token)
send_register_active_email.delay(email, username, token)  # 把任务放入队列

3.启动redis

 

 执行celery命令

celery -A celery_tasks.tasks worker -l info

报错

python@ubuntu:~/bj19/ttsx/dailyfresh$ celery -A celery_tasks.tasks worker -l info
Traceback (most recent call last):
  File "/usr/local/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.5/dist-packages/celery/__main__.py", line 16, in main
    _main()
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/celery.py", line 322, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/celery.py", line 496, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 298, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/celery.py", line 488, in handle_argv
    return self.execute(command, argv)
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/celery.py", line 420, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/worker.py", line 223, in run_from_argv
    return self(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/base.py", line 252, in __call__
    ret = self.run(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/celery/bin/worker.py", line 257, in run
    **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/celery/worker/worker.py", line 101, in __init__
    self.setup_instance(**self.prepare_args(**kwargs))
  File "/usr/local/lib/python3.5/dist-packages/celery/worker/worker.py", line 124, in setup_instance
    self.should_use_eventloop() if use_eventloop is None
  File "/usr/local/lib/python3.5/dist-packages/celery/worker/worker.py", line 243, in should_use_eventloop
    self._conninfo.transport.implements.asynchronous and
  File "/usr/local/lib/python3.5/dist-packages/kombu/connection.py", line 871, in transport
    self._transport = self.create_transport()
  File "/usr/local/lib/python3.5/dist-packages/kombu/connection.py", line 598, in create_transport
    return self.get_transport_cls()(client=self)
  File "/usr/local/lib/python3.5/dist-packages/kombu/transport/redis.py", line 1039, in __init__
    raise ImportError('Missing redis library (pip install redis)')
ImportError: Missing redis library (pip install redis)

这是因为pip没装redis

安装redis组件

pip install -U "celery[redis]"

再次敲命令就可以了

发送邮件时继续报错

[2019-09-25 17:41:28,074: INFO/MainProcess] Received task: celery_tasks.tasks.send_register_active_email[58be8ac2-4deb-4ca1-809b-6a88e9a2fc9f]  
[2019-09-25 17:41:28,085: ERROR/ForkPoolWorker-2] Task celery_tasks.tasks.send_register_active_email[58be8ac2-4deb-4ca1-809b-6a88e9a2fc9f] raised unexpected: ImproperlyConfigured('Requested setting EMAIL_FROM, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.',)
Traceback (most recent call last):
  File "/home/python/.virtualenvs/bj19/lib/python3.5/site-packages/celery/app/trace.py", line 382, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/python/.virtualenvs/bj19/lib/python3.5/site-packages/celery/app/trace.py", line 641, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/python/bj19/ttsx/dailyfresh/celery_tasks/tasks.py", line 24, in send_register_active_email
    sender = settings.EMAIL_FROM  # 发件人
  File "/home/python/.virtualenvs/bj19/lib/python3.5/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/home/python/.virtualenvs/bj19/lib/python3.5/site-packages/django/conf/__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_FROM, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

django没有初始化导致的

在tasks.py文件中初始化

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dailyfresh.settings")  # 从wsgi.py中复制过来
django.setup()

再次测试,发送成功

posted on 2019-09-25 17:50  BZ易风  阅读(330)  评论(1编辑  收藏  举报