Celery学习---Celery 与django结合实现计划任务功能
项目的目录结构:
项目前提: 安装并启动Redis
安装Django和Celery的定时任务插件
安装方法一: pip直接安装【安装了pip的前提下】
omc@omc-virtual-machine:~$ pip3 install django-celery-beat
安装方法二:直接源安装
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple django-celery-beat
CeleryTest/settings.py
INSTALLED_APPS = [ ... 'app01', # 注册app 'django_celery_beat', # Celery的定时任务和Django结合 ] MIDDLEWARE = [ ... # 'django.middleware.csrf.CsrfViewMiddleware', ... ] STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),) # 现添加的配置,这里是元组,注意逗号 TEMPLATES = [ ... 'DIRS': [os.path.join(BASE_DIR, 'templates')], ] # for celery CELERY_BROKER_URL = 'redis://192.168.2.105', CELERY_BACKEND_URL = 'redis://192.168.2.105', # 用于Celery的返回结果的接收
插件利用创建表来管理定时任务的
omc@omc-virtual-machine:~/Celery/CeleryTest$ cd /home/omc/Celery/CeleryTest omc@omc-virtual-machine:~/Celery/CeleryTest$ python3 manage.py migrate
CeleryTest/urls.py
from django.contrib import admin from django.urls import path from django.conf.urls import url, include from app01 import views urlpatterns = [ path('admin/', admin.site.urls), url(r'index/', views.Index), ]
app01/tasks.py 文件名必须为tasks.py
# 文件名必须为tasks.py,Djaogo才能发现Celery from __future__ import absolute_import, unicode_literals from celery import shared_task # Django starts so that the @shared_task decorator (mentioned later) will use it: @shared_task # Django的各个App里都可以导入这个任务,否则只能在app01这个Django的App内使用 def add(x, y): return x + y @shared_task def mul(x, y): return x * y @shared_task def xsum(numbers): return sum(numbers)
CeleryTest/celery.py 文件名必须为celery.py
from __future__ import absolute_import, unicode_literals import os from celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'CeleryTest.settings') app = Celery('CeleryTest') # Using a string here means the worker don'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() @app.task(bind=True) def debug_task(self): print('Request: {0!r}'.format(self.request))
CeleryTest/__init__.py
from __future__ import absolute_import, unicode_literals # 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 __all__ = ['celery_app']
上传文件到Linux服务器【Linux服务器需安装好Django服务】
需要Ubuntu下安装Django
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple django
在Linux的后台启动任务:
omc@omc-virtual-machine:~/Celery/CeleryTest$ python3 manage.py runserver 0.0.0.0:9000
在Linux的后台创建super用户:
omc@omc-virtual-machine:~/Celery/CeleryTest$ cd /home/omc/Celery/CeleryTest omc@omc-virtual-machine:~/Celery/CeleryTest$ python3 manage.py createsuperuser
在Win7下浏览器访问:
http://192.168.2.105:9000/admin/
在admin页面里,多了4张表
添加存储计划:页面之间添加即可
Intervals表创建一个10秒的任务
添加一个periodictask任务
注意:到此只是存储了一个定时计划而已,执行不了
Celery-beat调度器从Django内读取任务后发布给Celery的worker去执行
Linux后台启动Celery的worker
omc@omc-virtual-machine:~/Celery/CeleryTest$ cd /home/omc/Celery/CeleryTest omc@omc-virtual-machine:~/Celery/CeleryTest$ celery -A CeleryTest worker -l info
启动Celery-beat的调度器:从django内读取定时任务让后台worker执行
omc@omc-virtual-machine:/etc/redis$ cd /home/omc/Celery/CeleryTest/ omc@omc-virtual-machine:~/Celery/CeleryTest$ celery -A CeleryTest beat -l info -S django
右侧定时调取,左侧执行任务
简单梳理一下:后台上传Django和Celery整合后的文件,配置好Celery-beat后,在Linux后台启动Django项目,从Win7下访问admin进入页面后有一个定时任务的配置页面,配置好了页面的定时任务后,在Linux后台启动Celery的worker, 启动Celery-beat的调度器。此时调度器从Django内的schedule表中获取任务同时调用Celery的worker去执行任务,从而达到定时执行任务的效果。
问题解决
跨机器访问[Win7访问Linux下Django服务]:
问题现象一:直接无法访问:
# 9000端口仅仅监测本机的连接 omc@omc-virtual-machine:~$ netstat -an|grep 9000
问题定位: Django启动方式错误
问题解决: 重新启动Django项目
错误的:python3 manage.py runserver 9000 【少了定向IP】 正确的:python3 manage.py runserver 0.0.0.0:9000
问题现象二:Win7下访问Linux下的Django和Celery结合的项目
问题解决:
更改settings.py里面的ALLOWED_HOSTS配置后重启服务
ALLOWED_HOSTS = ['*'] # 所有都可以访问
问题二:
问题现象: AttributeError: module 'CeleryTest' has no attribute 'celery'
问题定位: 1.未进入到项目内,所以无法找到命名为celery的文件
2.项目内没有一个命名为celery.py的文件
-------------------------------------------
个性签名: 所有的事情到最後都是好的,如果不好,那說明事情還沒有到最後~
本文版权归作者【小a玖拾柒】和【博客园】共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!