Celery, database table

Celery asynchronous tasks

When the file is executed, the directory where the file is located will be added to the environment variable

When performing relative import with ".", The file where the import statement is located cannot be an execution file

Add tasks via script

'''
# ...\luffyapi\scripts\celery\celery_task\celery.py
from celery import Celery

app = Celery(broker='redis://localhost:6379/0', backend='redis://localhost:6379/1', include=['celery_task.tasks'])


# ...\luffyapi\scripts\celery\celery_task\tasks.py
from .celery import app


@app.task  
def task1(x, y):
    print('task1')
    return 'task1_res: %s' % str(x + y)


@app.task
def task2(a, b):
    print('task2')
    return 'task2_res: %s' % str(a - b)
    

# 在cmd终端cd到: ...\luffyapi\scripts\celery, 然后执行: celery worker -A celery_task -l info -P gevent 启动worker服务


# ...\luffyapi\scripts\celery\add_task.py
from celery_task.tasks import task1, task2
from datetime import datetime, timedelta

task1.apply_async(args=(10, 20))
task2.apply_async(args=(10, 20), eta=datetime.utcnow() + timedelta(seconds=10))


# ...\luffyapi\scripts\celery\get_results.py
from celery_task.celery import app
from celery.result import AsyncResult

async = AsyncResult(id="6dd3fdaf-ef6f-4fd9-9de9-48b39562fc57", app=app)

if async.successful():
    result = async.get()
    print(result)
'''

Start a new beat service to add tasks

'''
# ...\luffyapi\celery_task\celery.py
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "luffyapi.settings.dev_settings")

from celery import Celery

app = Celery(broker='redis://127.0.0.1:6379/0', backend='redis://127.0.0.1:6379/1', include=['celery_task.tasks'])

app.conf.timezone = 'Asia/Shanghai'  # 设置时区

# from celery.schedules import crontab
from datetime import timedelta

app.conf.beat_schedule = {  # Scheduler: Sending due task xxx (celery_task.tasks.update_banner_cache)
    'xxx': {
        # 'schedule': crontab(hour=8, day_of_week=1),  # 每周一早八点
        'task': 'celery_task.tasks.update_banner_cache',
        'schedule': timedelta(seconds=10),
        'args': (),
    },
}


# ...\luffyapi\celery_task\tasks.py
from .celery import app
from luffyapi.apps.home.models import Banner
from luffyapi.apps.home.serializers import BannerModelSerializer
from django.conf import settings
from django.core.cache import cache


# 使用celery添加任务周期性更新django缓存
@app.task
def update_banner_cache():
    banner_queryset = Banner.objects.filter(is_delete=False, is_show=True).order_by('-orders').all()[:settings.BANNER_AMOUNT]
    banner_ser = BannerModelSerializer(banner_queryset, many=True)

    for banner in banner_ser.data:
        banner['image'] = "%s%s" % (settings.BASE_URL, banner.get('image'))  # BannerModelSerializer类根据request对象拼接后端url根路径

    cache.set('banner_cache', banner_ser.data)
    return True


# 打开一个cmd, cd到: E:\luffy\luffyapi, 然后执行: celery worker -A celery_task -l info -P gevent 启动worker服务
# 打开另一个cmd, cd到: E:\luffy\luffyapi, 然后执行: celery beat -A celery_task -l info 启动beat服务
'''

Database table

Free class, practical class, light class is divided into three tables, rather than put in a table, establish an abstract base table to store public fields

Guess you like

Origin www.cnblogs.com/-406454833/p/12709326.html