前言: 针对高延时任务, 直接在一次网络请求中处理完毕会导致很不好的体验, celery则可以不阻塞请求后台处理这些任务, 并且可以使用django的models进行数据库操作.

环境

python models:

  • celery-4.1.1
  • redis-2.10.6
  • django-1.11.7

其他:

  • redis-3.2.9
  • macos
  • python3.6

创建django工程

django-admin startproject dc
cd dc
django-admin startapp main

此时项目结构如下

dc
|-- __init__.py
|-- main
|   |-- __init__.py
|   |-- admin.py
|   |-- apps.py
|   |-- migrations
|   |   `-- __init__.py
|   |-- models.py
|   |-- tests.py
|   `-- views.py
|-- settings.py
|-- urls.py
`-- wsgi.py

修改settings.py, 添加app

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','dc.main' //new added
]

修改dc/main/models.py, 创建新models

from django.db import models# Create your models here.class Person(models.Model):first_name = models.CharField(max_length=30)last_name = models.CharField(max_length=30)

创建根访问节点

dc/main/views.py

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.def hello(request):return HttpResponse('hello world')

dc/urls.py

urlpatterns = [url(r'^admin/', admin.site.urls),url(r'^$', hello) //new added
]

依次执行以下语句, 初始化django各功能模块

python manage.py migrate
python manage.py makemigrations main
python manage.py sqlmigrate main 0001
python manage.py migrate

接下来python manage.py runserver, 访问http://127.0.0.1:8000 即可看到hello world.

启动redis

redis是作为celery中间件使用的, 用来存储消息队列.

redis解压后, 直接运行src/redis-server即可启动, 默认端口6379

配置celery

参考文档

创建dc/celery.py

#-*- coding:utf-8 -*-import os
from celery import Celery# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dc.settings')app = Celery('dc')# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

修改 dc/__init__.py

from .celery import app as celery_app__all__ = ['celery_app']

修改 dc/settings.py, 设定 redis URL

CELERY_BROKER_URL = 'redis://127.0.0.1:6379/3'

创建dc/main/tasks.py

#-*- coding:utf-8 -*-from celery import shared_task@shared_task
def test():import timetime.sleep(5)from dc.main.models import Personperson = Person(first_name='smith', last_name='jhon')person.save()c = Person.objects.count()print(f'person count is {c}')

修改dc/main/views.py

def hello(request):from dc.main.tasks import testtest.delay()return HttpResponse('hello world')

启动celery进程

celery -A dc worker -l info

接下来访问http://127.0.0.1:8000, 即可发现页面立刻返回, 并没有被time阻塞, 查看启动celery的窗口, 即可发现log以及打印的信息, 确定models可以正常使用.

おわり.

在django中使用celery相关推荐

  1. django中使用celery简单介绍

    链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载. 本章节我们重点在于实现,如何存储任务的结果. 我们将任务函数改为: from celery_demo.celery im ...

  2. Django 中使用Celery实现异步任务

    1.在settings.py 同级目录下,新增一个celery.py的文件 需要注意的是:你的项目目录名要和配置一样 例如我的项目目录名就是 base_django_api 我的目录结构如下: # c ...

  3. Django中使用Celery

    创建Django项目:略过 配置python 多任务一定要写celery celery配置 视图中: 自己配置URL: 启动worker celery worker -A celery_task -l ...

  4. Django中使用Celery(附赠代码)

    一.简介 Celery是由Python开发.简单.灵活.可靠的分布式任务队列,其本质是生产者消费者模型,生产者发送任务到消息队列,消费者负责处理任务.Celery侧重于实时操作,但对调度支持也很好,其 ...

  5. celery异步执行任务在Django中的应用实例

    1. 创建django项目celery_demo, 创建应用demo: django-admin startproject celery_demo python manage.py startapp ...

  6. Django中Celery简介

    初识Celery: Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理,可将一些耗时的任务放入该消息队列中处理,一些定时任务也可以放入队列中自动执行 ...

  7. 任务队列:celery快速入门及django中celery的用法

    文章目录 一.celey的简介 1.1 celery的工作机制 1.2 安装celery(5.2版本) 二.celery快速入门 2.1 选择broker 2.2 celery的简单使用 2.2.1 ...

  8. Django 中celery的使用

    1.django应用Celery django框架请求/响应的过程是同步的,框架本身无法实现异步响应. 但是我们在项目过程中会经常会遇到一些耗时的任务, 比如:发送邮件.发送短信.大数据统计等等,这些 ...

  9. django-celery中配置celery中的日志

    django-celery中配置celery中的日志 django配置文件中的logging只是针对django中的views.API起作用,对celery不起作用,也就是说celery不能使用dja ...

最新文章

  1. clion register
  2. [转载]迅速成长的两个重要途径
  3. caffe 测试时间报错 Aborted at unix time
  4. 分支限界法实现最优装载c++_分支限界法
  5. SAP HANA解读-2012 SAP商业同略会分享
  6. ABP VNext 微服务演示,项目状态和路线图
  7. Java网络编程 — Netty入门
  8. ThreadLocal工作原理和内存泄漏的预防
  9. Spring的消息 Java Message Service (JMS)
  10. GitLab地域封锁,总监愤而辞职!苹果产品路线图曝光;CAT 0.1.0发布|极客头条...
  11. boost::bind with ros action,ros中SimpleActionServer用boost::bind绑定多个参数
  12. php中type的格式,javascript – 在php中动态生成的输入[type = date]格式值
  13. J2ME 模拟短信发送界面开发程序
  14. 提前防止Non-PIE错误,检测app是否包含PIE标志
  15. [论文笔记]JED:Towards Real-Time Multi-Object Tracking
  16. 【数学建模常用模型】分类专题
  17. 东线报接口 全网一手线报全网(京东,淘宝,天猫)最全优惠信息
  18. ab变频器22b用户手册_AB变频器22B-D4P0N104
  19. 松弛型内存模型(Relaxed memory models)的一个有趣的例子
  20. android 热点 连接电脑上网,安卓手机做wifi热点让笔记本上网的方法

热门文章

  1. 大唐发电厂计算机考试题,全国计算机等级考试上机考试与题库解析:一级B
  2. 服务器是什么系统_服务器自愈路由系统、单线以及BGP多线的区别是什么?
  3. 【TensorFlow-windows】keras接口——ImageDataGenerator裁剪
  4. 编程书单:十本Python编程语言的入门书籍
  5. sql 日期类型空值等于 1900-01-01
  6. [Xcode 实际操作]六、媒体与动画-(6)使用UIBlurEffect给图片添加模糊效果
  7. 支付宝APP支付 统一下单 php服务端 tp5
  8. 49、剑指offer--把字符串转换成整数
  9. PHP-CGI, FastCGI, PHP-FPM的关系和区别
  10. vue-resource 拦截器(interceptor)的使用