1. 程式人生 > >Celery學習---Celery 與django結合實現計劃任務功能

Celery學習---Celery 與django結合實現計劃任務功能

namespace display tor 必須 hat pip port 1-1 啟動

項目的目錄結構:

技術分享圖片

項目前提: 安裝並啟動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的文件

Celery學習---Celery 與django結合實現計劃任務功能