Django+Celery配置流程

环境

Python 3.6
Django 2.1.5
Celery 3.1.26
Django-Celery 3.2.2

一、创建django项目

创建项目:djtest1
django-admin startproject djtest1
创建APP:apps1
cd djtest1
python manage.py startapp apps1

标题二、配置django

修改django的配置文件 setting.py
增加和修改如下:

    djcelery.setup_loader()  ###
    CELERY_TIMEZONE='Asia/Shanghai'  #并没有北京时区,与下面TIME_ZONE应该一致
    BROKER_URL='redis://192.168.217.77:16379/8'  #任何可用的redis都可以,不一定要在django server运行的主机上
    CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'  ###
    
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'djcelery',    ### 加入djcelery应用
        'apps1',     ###     加入新创建的apps1
    )
    TIME_ZONE='Asia/Shanghai'  ### 

注:开头增加如上配置文件,根据实际情况配置redis的地址和端口,时区一定要设置为Asia/Shanghai。否则时间不准确回影响定时任务的运行。

上面代码首先导出djcelery模块,并调用setup_loader方法加载有关配置;注意配置时区,不然默认使用UTC时间会比东八区慢8个小时。其中INSTALLED_APPS末尾添加两项,分别表示添加celery服务和自己定义的apps服务。

三、编写celery文件

djtest1/djtest1/celery.py

#!/bin/python
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djtest1.settings')

#Specifying the settings here means the celery command line program will know where your Django project is. 
#This statement must always appear before the app instance is created, which is what we do next: 

app = Celery('djtest1')
app.config_from_object('django.conf:settings')

#This means that you don’t have to use multiple configuration files, and instead configure Celery directly from the Django settings.
#You can pass the object directly here, but using a string is better since then the worker doesn’t have to serialize the object.

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

#With the line above Celery will automatically discover tasks in reusable apps if you define all tasks in a separate tasks.py module.
#The tasks.py should be in dir which is added to INSTALLED_APP in settings.py. 
#So you do not have to manually add the individual modules to the CELERY_IMPORT in settings.py.

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))  #dumps its own request information

四、修改模块初始化配置

djtest1/djtest1/init.py

#!/bin/python
from __future__ import absolute_import

#This will make sure the app is always imported when
#Django starts so that shared_task will use this app.
from .celery import app as celery_app

五、编写tasks.py

from __future__ import absolute_import

from celery import task

from celery import shared_task

#from celery.task import tasks 
#from celery.task import Task 

@task()
#@shared_task
def add(x, y):
    print "%d + %d = %d"%(x,y,x+y)
    return x+y
#class AddClass(Task):
#    def run(x,y):
#        print "%d + %d = %d"%(x,y,x+y)
#        return x+y
#tasks.register(AddClass)

@shared_task
def mul(x, y):
    print "%d * %d = %d"%(x,y,x*y)
    return x*y


@shared_task
def sub(x, y):
    print "%d - %d = %d"%(x,y,x-y)
    return x-y

六、同步数据库

1.配置数据库
检查安装pymysql:pip install pymysql

2.注册驱动
在项目文件夹下的__init__.py添加代码如下:
import pymysql
pymysql.install_as_MySQLdb()

3.修改数据库连接本地数据库
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’, # 或者使用 mysql.connector.django
‘NAME’: ‘djtest1’,
‘USER’: ‘root’,
‘PASSWORD’: ‘aaa@bbb’,
‘HOST’:‘localhost’,
‘PORT’:‘3306’,
}
}

4.执行数据库迁移命令
python manage.py makemigrations
python manage.py migrate

七、创建超级用户

python manage.py createsuperuser

Username (leave blank to use ‘work’): admin
Email address: admin@test.com
Password: aaabbb
Password (again): aaabbb
Superuser created successfully.

八、启动redis、django-web、启动celery beat 启动 celery worker进程

#启动redis
redis-server.exe redis.windows.conf
#启动django的应用,可以动态的使用django-admin来管理任务
python manage.py runserver 0.0.0.0:8001
#应该是用来监控任务变化的
python manage.py celery beat
#任务执行进程,worker进程
python manage.py celery worker -c 6 -l debug

九、添加测试任务运行情况

http://localhost:8001/admin/
用刚刚注册的超级用户登陆进后台添加任务

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值