鱼C论坛

 找回密码
 立即注册
查看: 1119|回复: 1

celery异步发送任务,开启worker时一直报错

[复制链接]
发表于 2019-6-26 17:19:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
使用celery进行异步任务的发送,但开启worker时,一直报如下错,求大佬指点下(找了一下午了,都没解决)
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
我的代码如下

  1. # 使用celery
  2. from django.conf import settings
  3. from django.template import loader, RequestContext
  4. from celery import Celery
  5. from goods.models import GoodsType,IndexGoodsBanner,IndexPromotionBanner,IndexTypeGoodsBanner
  6. from django_redis import get_redis_connection

  7. import os
  8. import django
  9. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dailyfresh.settings")
  10. django.setup()

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

  13. @app.task
  14. def generate_static_index_html():
  15.     '''产生首页静态页面'''
  16.     # 获取商品的种类信息
  17.     types = GoodsType.objects.all()

  18.     # 获取首页轮播商品信息
  19.     goods_banners = IndexGoodsBanner.objects.all().order_by('index')

  20.     # 获取首页促销活动信息
  21.     promotion_banners = IndexPromotionBanner.objects.all().order_by('index')

  22.     # 获取首页分类商品展示信息
  23.     for type in types:  # GoodsType
  24.         # 获取type种类首页分类商品的图片展示信息
  25.         image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
  26.         # 获取type种类首页分类商品的文字展示信息
  27.         title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')

  28.         # 动态给type增加属性,分别保存首页分类商品的图片展示信息和文字展示信息
  29.         type.image_banners = image_banners
  30.         type.title_banners = title_banners


  31.     # 组织模板上下文
  32.     context = {'types': types,
  33.                'goods_banners': goods_banners,
  34.                'promotion_banners': promotion_banners}

  35.     # 使用模板
  36.     # 1.加载模板文件,返回模板对象
  37.     temp = loader.get_template('static_index.html')
  38.     # 2.模板渲染
  39.     static_index_html = temp.render(context)

  40.     # 生成首页对应静态文件
  41.     save_path = os.path.join(settings.BASE_DIR, 'static/index.html')
  42.     with open(save_path, 'w') as f:
  43.         f.write(static_index_html)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-26 20:10:02 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-16 13:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表