Using the timer task django-crontab of high frequency [static pages, increase the user experience] [timer system, independent of the implementation of the project] [Xinyu]

 

Static pages

Thinking:

  • Home page visit frequently, and query large volumes of data, of which there are a large number of processing cycles.

problem:

  • Home users access the server will spend a lot of resources, efficiency and response data will be greatly reduced.

solve:

  • Static pages

 

 

 

1. static pages Introduction

1. Why do static pages

  • Reducing the number of database queries.
  • Enhance the efficiency of the response page.

2. What is static pages

  • Save the generated pages dynamically render the results into html files into a static file server.
  • The user directly to the static server, access to a good deal of static html files.

2. Home page static achieve

1. Home page static implementation steps

  • Home-related data query
  • Gets Home template file
  • Home html rendering string
  • Home html string will be written to the specified directory named 'index.html'

2. Home page static achieve

import os
import time
from django.conf import settings
from django.template import loader
from apps.contents.models import ContentCategory
from apps.contents.utils import get_categories

def generate_static_index_html():
    """
    生成静态的主页html文件
    """
    print('%s: generate_static_index_html' % time.ctime())

    # 获取商品频道和分类
    categories = get_categories()

    # 广告内容
    contents = {}
    content_categories = ContentCategory.objects.all()
    for cat in content_categories:
        contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

    # 渲染模板
    context = {
        'categories': categories,
        'contents': contents
    }

    # 获取首页模板文件
    template = loader.get_template('index.html')
    # 渲染首页html字符串
    html_text = template.render(context)
    # 将首页html字符串写入到指定目录,命名'index.html'
    file_path = os.path.join(settings.STATICFILES_DIRS[0], 'index.html')
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(html_text)

3. regular tasks crontab static Home

important hint:

  • For static home page, the page may take into account the data maintained by a number of operational staff, and often change, so make it into regular tasks, namely the timing of the implementation of static.
  • Perform regular tasks in Django, can  django-crontab be achieved expansion.

1. Install django-crontab

$ pip install django-crontab 

2. Register django-crontab application

INSTALLED_APPS = [         'django_crontab', # 定时任务 ] 

3. Set up regular tasks

定时时间基本格式 :  *  *  *  *  *  分 时 日 月 周    命令  M: 分钟(0-59)。每分钟用 * 或者 */1 表示 H:小时(0-23)。(0表示0点) D:天(1-31)。 m: 月(1-12)。 d: 一星期内的天(0~6,0为星期天)。 

Timing task definitions divided into three parts:

  • Task time
  • Task Method
  • Task Log
CRONJOBS = [     # 每1分钟生成一次首页静态文件     ('*/1 * * * *', 'apps.contents.crons.generate_static_index_html', '>> ' + os.path.join(BASE_DIR, 'logs/crontab.log')) ] 

Solve the problem of Chinese crontab

  • In the regular task, if non-English characters appear, there will be character exception error
CRONTAB_COMMAND_PREFIX = 'LANG_ALL=zh_cn.UTF-8' 

4. Timing management tasks

# 添加定时任务到系统中  $ python manage.py crontab add   # 显示已激活的定时任务  $ python manage.py crontab show   # 移除定时任务  $ python manage.py crontab remove

 

Guess you like

Origin www.cnblogs.com/LiuXinyu12378/p/11332033.html