启动celery后执行任务报错:django.core.exceptions.ImproperlyConfigured

工作的环境版本如下:

【Django version】: 2.1

【celery version】:4.4.0rc2

【python version】: 3.7

【Redis version】:3.2.1

启动celery没有报错,但是执行队列任务的时候就爆出下的错误:

[2019-07-07 18:43:27,756: INFO/MainProcess] Connected to redis://127.0.0.1:6379/0
[2019-07-07 18:43:27,764: INFO/MainProcess] mingle: searching for neighbors
[2019-07-07 18:43:28,784: INFO/MainProcess] mingle: all alone
[2019-07-07 18:43:28,818: INFO/MainProcess] celery@DESKTOP-9T9MK4N ready.
[2019-07-07 18:43:28,818: INFO/MainProcess] pidbox: Connected to redis://127.0.0.1:6379/0.
[2019-07-07 18:43:40,217: INFO/MainProcess] Received task: celery_tasks.tasks.send_register_active_email[c4156f7a-ea3d-457d-8b18-40cc93bb718c]
[2019-07-07 18:43:40,219: ERROR/MainProcess] Task celery_tasks.tasks.send_register_active_email[c4156f7a-ea3d-457d-8b18-40cc93bb718c] raised unexpected: ImproperlyConfi
gured('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 "c:\users\circle\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\trace.py", line 385, in trace_task
    R = retval = fun(*args, **kwargs)
  File "c:\users\circle\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\trace.py", line 650, in __protected_call__
    return self.run(*args, **kwargs)
  File "C:\Users\Circle\Desktop\circle\dailyfresh\celery_tasks\tasks.py", line 25, in send_register_active_email
    sender = settings.EMAIL_FROM
  File "c:\users\circle\appdata\local\programs\python\python37-32\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "c:\users\circle\appdata\local\programs\python\python37-32\lib\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_SETTI
NGS_MODULE or call settings.configure() before accessing settings.

大概意思就是没有配置设置。您必须定义环境变量DJANGO_SETTINGS_MODULE在访问设置之前调用settings.configure()。

解决办法就是:

# 使用celery,首先导入进来
from celery import Celery
from django.conf import settings
from django.core.mail import send_mail

# 下面就是解决办法:将Django中的配置信息先导入进来
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dailyfresh.settings')
django.setup()

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


# 定义任务函数
@app.task
def send_register_active_email(email, username, token):
    """发送激活邮件"""
    subject = '欢迎观看本篇文章'
    message = '希望对你有帮助'
    sender = settings.EMAIL_FROM
    recipient_list = [email]
    html_message = '

The End

' send_mail(subject, message, sender, recipient_list, html_message=html_message)

 

转载于:https://www.cnblogs.com/Hannibal-2018/p/11147250.html

你可能感兴趣的:(python,数据库)