1.1 為什么有這篇博客
1.博客說明
1.在做celery異步任務和定時任務時,有些人使用django-celery+django-redis+celery+redis+django-celery-beat實現
2.但是這種實現方法和django結合過於緊密,不利於分布式部署
3.而且不同版本相結合,一旦不小心安裝升級一個包,會導致各種報錯
4.配置也比較繁瑣,很多同學在使用時易出錯
2、安裝相關包
pip install Django==2.2 pip install celery==4.4.7 pip install redis==3.5.3
1.2 celery基本使用
1、創建tasks.py文件進行驗證

from celery import Celery import time app = Celery('TASK', broker='redis://localhost', backend='redis://localhost') @app.task def add(x, y): print("running..add.", x, y) return x + y @app.task def minus(x, y): time.sleep(60) print("running..minus.", x, y) return x - y
1、啟動Celery Worker來開始監聽並執行任務
celery -A tasks worker --loglevel=info # tasks是tasks.py文件:必須在tasks.py所在目錄下執行
2、調用任務:再打開兩個終端,進行命令行模式,調用任務
>>> import tasks
>>> import tasks
>>> t2 = tasks.minus.delay(9,11)
#然后在另一個終端重復上面步驟執行
>>> t1 = tasks.add.delay(3,4)
>>> t1.get() #由於t2執行sleep了3s所以t1.get()需要等待
2、celery其他命令
>>> t.ready() #返回true證明可以執行,不必等待
>>> t.get(timeout=1) #如果1秒不返回結果就超時,避免一直等待
>>> t.get(propagate=False) #如果執行的代碼錯誤只會打印錯誤信息
>>> t.traceback #打印異常詳細結果
1.2 在django項目中使用
1、目錄結構如下
2、opwf_project/celery_task文件夾

# celery.py # -*- coding: utf-8 -*- from celery import Celery import os,sys import django # 1.添加django項目根路徑 CELERY_BASE_DIR = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../opwf')) # 2.添加django環境 os.environ.setdefault("DJANGO_SETTINGS_MODULE","opwf.settings") django.setup() # 讀取配置 # 3.celery基本配置 app = Celery('proj', broker='redis://localhost:6379/14', backend='redis://localhost:6379/15', include=['celery_task.tasks', 'celery_task.tasks2', ]) # 4.實例化時可以添加下面這個屬性 app.conf.update( result_expires=3600, #執行結果放到redis里,一個小時沒人取就丟棄 ) # 5.配置定時任務:每5秒鍾執行 調用一次celery_pro下tasks.py文件中的add函數 app.conf.beat_schedule = { 'add-every-5-seconds': { 'task': 'celery_task.tasks.test_task_crontab', 'schedule': 5.0, 'args': (16, 16) }, } # 6.添加時區配置 app.conf.timezone = 'UTC' if __name__ == '__main__': app.start()

# -*- coding:utf8 -*- from .celery import app #從當前目錄導入app import os,sys from .celery import CELERY_BASE_DIR # 1.test_task_crontab測試定時任務 @app.task def test_task_crontab(x, y): # 添加django項目路徑 sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../opwf')) from utils.rl_sms import test_crontab res = test_crontab(x, y) return x + y # 2.測試異步發送郵件 @app.task(bind=True) def send_sms_code(self, mobile, datas): sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../opwf')) # 在方法中導包 from utils.rl_sms import send_message # time.sleep(5) try: # 用 res 接收發送結果, 成功是:0, 失敗是:-1 res = send_message(mobile, datas) except Exception as e: res = '-1' if res == '-1': # 如果發送結果是 -1 就重試. self.retry(countdown=5, max_retries=3, exc=Exception('短信發送失敗'))

# -*- coding:utf8 -*- from .celery import app import time,random @app.task def randnum(start,end): time.sleep(3) return random.randint(start,end)
3、opwf_project/opwf/utils

# -*- coding: utf-8 -*- # utils/rl_sms.py from ronglian_sms_sdk import SmsSDK from user.models import User accId = '8a216da8747ac98201749c0de38723b7' accToken = '86072b540b4648229b27400414150ef2' appId = '8a216da8747ac98201749c0de45123be' def send_message(phone, datas): user = User.objects.all()[0] print(user.username, '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') sdk = SmsSDK(accId, accToken, appId) tid = '1' # 測試模板id為: 1. 內容為: 【雲通訊】您的驗證碼是{1},請於{2}分鍾內正確輸入。 # mobile = '13303479527' # datas = ('666777', '5') # 模板中的參數按照位置傳遞 # resp = sdk.sendMessage(tid, phone, datas) print("##########################################") print('執行了這個方法 send_message') return '' def test_crontab(x,y): print('############### 執行test_crontab測試任務 #############') print('############### 郵件審批超時提醒 #############')
4、在django項目中調用
def handle_next_suborder_approve(suborder_obj): # 函數內部導報
from celery_task import tasks # .delay()發送異步任務
tasks.send_sms_code.delay(18538752511,()) # 通知審判者
5、執行命令
### 1.1 進入執行目錄
cd opwf_project ### 1.2 celery管理
celery -A celery_task worker -l INFO # 單線程
celery multi start w1 w2 -A celery_pro -l info #一次性啟動w1,w2兩個worker
celery -A celery_pro status #查看當前有哪些worker在運行
celery multi stop w1 w2 -A celery_pro #停止w1,w2兩個worker
# 1.項目中啟動celery worker
celery multi start celery_task -A celery_task -l debug --autoscale=50,10 # celery並發數:最多50個,最少5個 # 2.在項目中關閉celery worker
ps auxww|grep "celery worker"|grep -v grep|awk '{print $2}'|xargs kill -9 # 關閉所有celery進程
``` ### 1.3 django_celery_beat管理 # 1.普通測試啟動celery beat
celery -A celery_task beat -l info # 2.在項目中后台啟動celery beat
celery -A celery_task beat -l debug >> /aaa/Scheduler.log 2>&1 &
# 3.停止celery beat
ps -ef | grep -E "celery -A celery_test beat" | grep -v grep| awk '{print $2}' | xargs kill -TERM &> /dev/null # 殺死心跳所有進程