创建生成商品列表的计划任务 创建计划任务 1.在计划任务包目录 celery_tasks 目录下新建一个名为 html 的包目录;
2.在新建的 html 包目录下新建一个 tasks.py 文件,内容为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import os from goods.utils import get_categories from celery_tasks.main import app from django.conf import settings from django.template import loader @app.task(name="generate_static_list_search_html" ) def generate_static_list_search_html(): "" " 生成静态的商品列表页 " "" categories = get_categories() context = { 'categories' : categories } template = loader.get_template('list.html' ) html_text = template.render(context) file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'list.html' ) with open(file_path, 'w' , encoding='utf-8' ) as f: f.write(html_text)
注册计划任务 1.编辑计划任务包目录 celery_tasks 目录下的 main.py 文件,在 app.autodiscover_tasks(..)
元祖中添加 html
计划任务;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ''' # @Time: 8/26/23 8:30 PM # @Author: leazhi # @Emal: [email protected] # @Filename: main.py # @Project: python3 ''' from celery import Celeryimport osif not os.getenv('DJANGO_SETTINGS_MODULE' ): os.environ['DJANGO_SETTINGS_MODULE' ] = 'haoke.settings.dev' app = Celery('haoke' ) app.config_from_object('celery_tasks.config' ) app.autodiscover_tasks(['celery_tasks.sms' , 'celery_tasks.email' , 'celery_tasks.html' ])
改写商品子应用 1.在 goods 子应用下新建一个 utils.py 的文件,将查询的商品列表部分代码抽出来,写入到该文件中,便于上面的计划任务调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 from collections import OrderedDictfrom .models import GoodsChanneldef get_categories (): categories = OrderedDict() channels = GoodsChannel.objects.order_by('group_id' , 'sequence' ) for channel in channels: group_id = channel.group_id if group_id not in categories: categories[group_id] = {'channel' : [], 'sub_cats' : []} cat1 = channel.category categories[group_id]['channel' ].append({ 'id' : cat1.id , 'name' : cat1.name, 'url' : channel.url }) for cat2 in cat1.goodscategory_set.all (): cat2.sub_cats = [] for cat3 in cat2.goodscategory_set.all (): cat2.sub_cats.append(cat3) categories[group_id]['sub_cats' ].append(cat2) return categories
调用商品列表页异步任务 1.编辑子应用 goods 下面的 admin.py 文件,在该文件中添加商品修改类,然后在该类中调用生成商品列表异步任务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 goods/admin.py from django.contrib import adminfrom celery_tasks.html.tasks import generate_static_list_search_html from .models import *class GoodsCategoryAdmin (admin.ModelAdmin): def save_model (self, request, obj, form, change ): """ 实现修改和添加模型类数据 obj:获取修改或者添加的数据 """ obj.save() generate_static_list_search_html() def delete_model (self, request, obj ): obj.delete() generate_static_list_search_html() admin.site.register(Goods, GoodsCategoryAdmin) admin.site.register(Brand) admin.site.register(SKU) admin.site.register(SKUImage) admin.site.register(SKUSpecification) admin.site.register(GoodsChannel) admin.site.register(GoodsCategory) admin.site.register(GoodsSpecification) admin.site.register(SpecificationOption)
在命令行测试调用异步任务 打开命令行终端,进入项目跟目录,然后执行;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ┌──(leazhi㉿kali-desktop)-[/data/gitlab/python3-django-small_haoke/haoke] └─$ celery -A celery_tasks.main worker -l info -------------- celery@kali-desktop v5.3.4 (emerald-rush) --- ***** ----- -- ******* ---- Linux-6.4.0-kali3-amd64-x86_64-with-glibc2.37 2023-09-20 08:14:50 - *** --- * --- - ** ---------- [config] - ** ---------- .> app: haoke:0x7faea6b4fb90 - ** ---------- .> transport: redis://192.168.3.254:22652/14 - ** ---------- .> results: disabled:// - *** --- * --- .> concurrency: 16 (prefork) -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker) --- ***** ----- -------------- [queues] .> celery exchange=celery(direct) key=celery [tasks] . generate_static_list_search_html . send_sms_code . send_varify_email [2023-09-20 08:14:51,011: WARNING/MainProcess] /home/leazhi/.local/lib/python3.11/site-packages/celery/worker/consumer/consumer.py:507: CPendingDeprecationWarning: The broker_connection_retry configuration setting will no longer determine whether broker connection retries are made during startup in Celery 6.0 and above. If you wish to retain the existing behavior for retrying connections on startup, you should set broker_connection_retry_on_startup to True. warnings.warn( ...
参数说明 :
worker -l info:指定调试模式下输入的日志级别;
采用异步任务的方法改写首页 1.编辑商品子应用 goods 目录下爱的 utils.py 文件,将首页静态化的方法从子应用你哦个 contents 目录下的 crons 文件中抽离代该文件中,并 return:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 from collections import OrderedDictfrom .models import GoodsChannelfrom ..contents.models import ContentCategorydef get_categories (): categories = OrderedDict() channels = GoodsChannel.objects.order_by('group_id' , 'sequence' ) for channel in channels: group_id = channel.group_id if group_id not in categories: categories[group_id] = {'channel' : [], 'sub_cats' : []} cat1 = channel.category categories[group_id]['channel' ].append({ 'id' : cat1.id , 'name' : cat1.name, 'url' : channel.url }) for cat2 in cat1.goodscategory_set.all (): cat2.sub_cats = [] for cat3 in cat2.goodscategory_set.all (): cat2.sub_cats.append(cat3) categories[group_id]['sub_cats' ].append(cat2) return categories def get_index_context (): categories = OrderedDict() channels = GoodsChannel.objects.order_by('group_id' , 'sequence' ) for channel in channels: group_id = channel.group_id if group_id not in categories: categories[group_id] = {'channel' : [], 'sub_cats' : []} cat1 = channel.category categories[group_id]['channel' ].append({ 'id' : cat1.id , 'name' : cat1.name, 'url' : channel.url }) for cat2 in cat1.goodscategory_set.all (): cat2.sub_cats = [] for cat3 in cat2.goodscategory_set.all (): cat2.sub_cats.append(cat3) categories[group_id]['sub_cats' ].append(cat2) contents = {} content_categories = ContentCategory.objects.all () for cat in content_categories: contents[cat.key] = cat.content_set.filter (status=True ).order_by('sequence' ) context = { 'contents' : contents, 'catetories' : categories } return context
2.编辑异步任务 html 包目录下的 tasks.py 文件,添加首页静态化任务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import osfrom haoke.apps.goods.utils import get_categoriesfrom celery_tasks.main import appfrom django.conf import settingsfrom django.template import loaderfrom haoke.apps.goods.utils import get_index_context@app.task(name="generate_static_list_search_html" ) def generate_static_list_search_html (): """ 生成静态的商品列表页 """ categories = get_categories() context = { 'categories' : categories } template = loader.get_template('list.html' ) html_text = template.render(context) file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'list.html' ) with open (file_path, 'w' , encoding='utf-8' ) as f: f.write(html_text) @app.task(name="generate_static_index_html" ) def generate_static_index_html (): context = get_index_context() template = loader.get_template('index.html' ) html_text = template.render(context) file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, 'index.html' ) with open (file_path, 'w' , encoding='utf-8' ) as f: f.write(html_text)
3.编辑子应用 goods 目录下的 admin.py 文件,在该文件中的 GoodsCategoryAdmin() 类中调用首页静态化的异步任务 generate_static_index_html():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 from django.contrib import adminfrom celery_tasks.html.tasks import generate_static_list_search_html, generate_static_index_htmlfrom .models import *class GoodsCategoryAdmin (admin.ModelAdmin): def save_model (self, request, obj, form, change ): """ 实现修改和添加模型类数据 obj:获取修改或者添加的数据 """ obj.save() generate_static_list_search_html() generate_static_index_html() def delete_model (self, request, obj ): obj.delete() generate_static_list_search_html() generate_static_index_html() admin.site.register(Goods, GoodsCategoryAdmin) admin.site.register(Brand) admin.site.register(SKU) admin.site.register(SKUImage) admin.site.register(SKUSpecification) admin.site.register(GoodsChannel) admin.site.register(GoodsCategory) admin.site.register(GoodsSpecification) admin.site.register(SpecificationOption)