解决celery激活worker端AttributeError问题

之前使用django 4配合celery 5.26的时候还好好的,今天新增了一个任务就突然不行了,一直报AttributeError,调了差不多24小时终于发现问题了。

对比任务端和worker端的代码可以发现,如果想要worker端的celery正常运行,就必须要把导入类的语句放到配置好环境变量之后,不然worker端的celery会找不到别的包,导致AttributeError。我由于在新增的任务中使用了工程中其他的类,又把导入类的语句放错了位置,所以导致了这个问题,如果你出现类似的问题,你也可以参考我的修改过程,少走弯路。一些编辑器的自动格式化会自动把导入语句放最前面,最好在关闭之后用记事本打开看看语句的顺序有没有错,不然会在保存的一瞬间自动格式化了。

worker端正确代码如下,仅供参考:

# __init__.py
# celery worker端初始化代码,可以不放tasks.py里面,这里也可以读取
import os
import django
# worker端的环境变量配置
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'daily_fresh.settings')
django.setup()

# worker端启动代码(先切换目录)
# celery --app=celery_tasks.tasks worker -l INFO

# tasks.py
import os
from celery import Celery               # 使用celery
from daily_fresh import settings
from django.template import loader
from django.core.mail import send_mail

from goods.models import *     # worker端这一句放在环境变量之后,不然celery会报错


# 创建一个Celery类的实例对象,链接redis,用1号数据库
app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/1')


@app.task   # 使用task修饰器创建被celery调用的任务
def send_register_avtive_email(to_email, username, token):  # 定义任务函数
    """发送激活邮件"""
    subject = "天天生鲜欢迎信息"     # 邮件标题
    message = '邮件正文'            # 邮件正文(用了html-message就不会显示了)
    html_message = '<h1>%s,欢迎您成为天天生鲜注册会员</h1>'\
        '请点击下面链接激活您的账户,半小时内有效:<br>'     \
        '<a href="http://127.0.0.1:8000/user/active/%s">'\
        'http://127.0.0.1:8000/user/active/%s<a/>'       \
        % (username, token, token)
    sender = settings.EMAIL_FROM    # 发件人
    receiver = [to_email]           # 收件人
    send_mail(subject, message, sender, receiver,
              html_message=html_message)


@app.task
def generate_static_index_html():
    """产生首页静态页面"""
    # 查询商品的种类信息
    types = GoodsType.objects.all()
    # 获取首页轮播的商品的信息
    index_banner = IndexGoodsBanner.objects.all().order_by('index')
    # 获取首页促销的活动信息
    promotion_banner = IndexPromotionBanner.objects.all().order_by('index')

    # 获取首页分类商品信息展示
    for type in types:
        # 查询首页显示的type类型的文字商品信息
        title_banner = IndexTypeGoodsBanner.objects.filter(
            type=type, display_type=0).order_by('index')
        # 查询首页显示的图片商品信息
        image_banner = IndexTypeGoodsBanner.objects.filter(
            type=type, display_type=1).order_by('index')
        # 动态给type对象添加两个属性保存数据
        type.title_banner = title_banner
        type.image_banner = image_banner

    # 组织模板上下文
    context = {
        'types': types,
        'index_banner': index_banner,
        'promotion_banner': promotion_banner,
    }

    # 加载模板文件,返回模板对象
    temp = loader.get_template('static_index.html')
    # 渲染模板
    static_index_html = temp.render(context)

    # 生成首页对应静态文件
    save_path = os.path.join(settings.BASE_DIR, 'static/index.html')
    with open(save_path, 'w') as f:
        f.write(static_index_html)

dkl@dkl-VMware-Virtual-Platform:~/learn/graph/study_celery$ celery -A celery_task worker -I info Usage: celery [OPTIONS] COMMAND [ARGS]... Try 'celery --help' for help. Error: Invalid value for '-A' / '--app': Unable to load celery application. While trying to load the module celery_task the following error occurred: Traceback (most recent call last): File "/home/dkl/learn/env/lib/python3.12/site-packages/celery/app/utils.py", line 389, in find_app found = sym.app ^^^^^^^ AttributeError: module 'celery_task' has no attribute 'app' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/dkl/learn/env/lib/python3.12/site-packages/celery/app/utils.py", line 394, in find_app found = sym.celery ^^^^^^^^^^ AttributeError: module 'celery_task' has no attribute 'celery' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/dkl/learn/env/lib/python3.12/site-packages/celery/app/utils.py", line 383, in find_app sym = symbol_by_name(app, imp=imp) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dkl/learn/env/lib/python3.12/site-packages/kombu/utils/imports.py", line 64, in symbol_by_name return getattr(module, cls_name) if cls_name else module ^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: module 'celery_task' has no attribute 'celery' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/dkl/learn/env/lib/python3.12/site-packages/celery/bin/celery.py", line 58, in convert return find_app(value) ^^^^^^^^^^^^^^^ File "/home/dkl/learn/env/lib/python3.12/site-packages/celery/app/utils.py", line 401, in find_app return find_app( ^^^^^^^^^ File "/home/dkl/learn/env/lib/python3.12/site-packages/celery/app/utils.py", line 386, in find_app sym = imp(app) ^^^^^^^^ File "/home/dkl/learn/env/lib/python3.12/site-packages/celery/utils/imports.py", line 109, in import_from_cwd return imp(module, package=package) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1387, in _gcd_import File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 995, in exec_module File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed File "/home/dkl/learn/graph/study_celery/celery_task/celery.py", line 30, in <module> 'schedule': crontab(seconds=10), ^^^^^^^^^^^^^^^^^^^ File "/home/dkl/learn/env/lib/python3.12/site-packages/celery/schedules.py", line 409, in __init__ super().__init__(**kwargs) TypeError: BaseSchedule.__init__() got an unexpected keyword argument 'seconds'
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值