码迷,mamicode.com
首页 > 其他好文 > 详细

djcelery执行异步任务和定时任务

时间:2020-01-23 09:49:33      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:ack   *args   技术   opened   ORC   ade   from   turn   pen   

技术图片
import djcelery
from datetime import timedelta

djcelery.setup_loader()

CELERY_QUEUES = {
    beat_tasks:{
        exchange:beat_tasks,
        exchange_type:direct,
        binding_key:beat_tasks,
    },
    work_queue: {
        exchange: work_queue,
        exchange_type: direct,
        binding_key: work_queue,
    }
}

CELERY_DEFAULT_QUEUE = work_queue
CELERY_IMPORTS = (
    app01.tasks,
)
# 有些情况可以防止死锁
CELERYD_FORCE_EXECV = True
# 设置并发worker数量
CELERYD_CONCURRENCY = 4
# 允许重试
CELERY_ACKS_LATE = True
# 每个worker最多执行100个任务被销毁,可以防止内存泄漏
CELERYD_MAX_TASKS_PER_CHILD = 100
# 超时时间
CELERYD_TASK_TIME_LIMIT = 12 * 30

CELERYBEAT_SCHEDULE = {
    task1:{
        task:course-task,
        schedule:timedelta(seconds=5),
        options:{
            queue:beat_tasks
        }
    }
}
celeryconfig.py
技术图片
# from celery import task
import time
# @task
# def add(x,y):
#     time.sleep(3)
#     return x+y

from celery.task import Task

class CourseTask(Task):
    name = course-task

    def run(self, *args, **kwargs):
        print(start course task)
        time.sleep(4)
        print(args={}, kwargs={}.format(args, kwargs))
        print(end course task)
tasks.py
技术图片
from app01.tasks import CourseTask
from django.http import JsonResponse
def do(request):
    print(start do request)
    # CourseTask.delay()
    CourseTask.apply_async(args=(hello,),queue=work_queue)
    print(end do request)
    return JsonResponse({result:ok})
views.py

djcelery执行异步任务和定时任务

标签:ack   *args   技术   opened   ORC   ade   from   turn   pen   

原文地址:https://www.cnblogs.com/xuqidong/p/12229999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!