Django使用异步任务与定时任务
Django配置
创建项目
# tree -L 2
.
├── djtest
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
##### 创建 worker 目录
.
├── djtest
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── worker
├── config.py
└── __init__.py
异步耗时任务
配置文件worker/config.py
# Celery 配置
broker_url = 'redis://127.0.0.1:6379/9'
broker_pool_limit = 50 # Borker 连接池, 默认是10
timezone = 'Asia/Shanghai'
accept_content = ['pickle', 'json']
task_serializer = 'pickle'
result_expires = 3600 # 任务过期时间
result_backend = 'redis://127.0.0.1:6379/9'
result_serializer = 'pickle'
result_cache_max = 100 # 任务结果最大缓存数量
worker_redirect_stdouts_level = 'INFO'
worker/__init__.py
import os
from celery import Celery, platforms
platforms.C_FORCE_ROOT = True # 加上这一行
from worker import config
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djtest.settings")
celery_app = Celery('worker')
celery_app.config_from_object(config)
celery_app.autodiscover_tasks()
app/tasks.py
from __future__ import absolute_import, unicode_literals
from worker import celery_app
import time
@celery_app.task
def consume_task(s):
print('this is a consume task'.center(30, '-'))
time.sleep(s)
print('this is a consume task'.center(30, '-'))
return True
views.py
from django.shortcuts import render
from django.http import JsonResponse
from cache import tasks
def index(request):
# do something
res = tasks.consume_task.delay(3)
return JsonResponse({'status': 'successful', 'task_id': res.task_id})
启动celery
celery -A worker worker --loglevel=info
测试
请求接口
127.0.0.1:6379[9]> keys *
1) "celery-task-meta-3775a6a8-7275-4c8a-8164-b3d8727407b8"
2) "unacked_mutex"
3) "_kombu.binding.celery.pidbox"
4) "celery-task-meta-468aee54-0df1-4c3d-802c-8924be9eda4b"
5) "_kombu.binding.celery"
6) "_kombu.binding.celeryev"
127.0.0.1:6379[9]> get celery-task-meta-3775a6a8-7275-4c8a-8164-b3d8727407b8
"{\"status\": \"SUCCESS\", \"result\": \"this is task result\", \"traceback\": null, \"children\": [], \"date_done\": \"2020-02-16T02:25:34.092003\", \"task_id\": \"3775a6a8-7275-4c8a-8164-b3d8727407b8\"}"
网友评论