15Django中使用celery之測試環境和正式環境
阿新 • • 發佈:2021-12-15
django使用celery的步驟:
1建立celery配置檔案編寫初始化指令碼
2在各自的應用下分別建立自己的tasks.py檔案,用於集中定義對應worker函式
3檢視函式充當生產者,推送具體worker函式
4專案目錄下啟動worker
celery -A 專案同名目錄名 worker -l info
示例:
1在專案同名目錄下建立celery配置檔案celery.py《與setting.py平級》
linuxTangblog dtoken linuxTangblog __init__.py asgi.py celery.py settings.py urls.py views.py wsgi.py media
2在celery.py中編寫初始化:
from celery import Celery from django.conf import settings import os #在linux環境變數里加一個特殊的環境變數,特殊變數DJANGO_SETTINGS_MODULE相當於key,而專案名稱.settings相當於value #其實就是告訴celery你要和專案linuxTangblog裡的settings進行融合 os.environ.setdefault('DJANGO_SETTINGS_MODULE','linuxTangblog.settings') #初始化celery,名字可以隨便起,我就起一個和專案同名的名稱app = Celery('linuxTangblog') #初始化配置 app.conf.update( BROKER_URL = 'redis://:@127.0.0.1:6379/1' ) #自動去註冊應用下尋找載入worker函式 app.autodiscover_tasks(settings.INSTALLED_APPS)
3在應用user下建立tasks.py:
linuxTangblog dtoken linuxTangblog media tools user migrations __init__.py admin.py apps.py models.py tasks.py tests.py urls.py
4把user應用視圖裡編寫的傳送簡訊的方法貼上到task.py裡:
from linuxTangblog.celery import app from django.conf import settings from tools.sms import Yuntongxin #發簡訊 @app.task def send_sms_c(phone,code): config = { "accountSid": settings.SMS_ACCOUNT_SID, "accountToken": settings.SMS_ACCOUNT_TOKEN, "appId": settings.SMS_APPID, "templateId": settings.SMS_TEMPLATEID, } yun = Yuntongxin(**config) res = yun.run(phone, code) return res
5回到使用者檢視函式中把task.py裡的函式send_sms_c引進來,然後用send_sms_c替換掉send_sms
from .tasks import send_sms_c #發簡訊邏輯檢視 def sms_view(request): #傳送隨機碼 #send_sms(phone,code) send_sms_c.delay(phone,code)
6cmd進入到專案目錄,確保能看到manage.py這個檔案然後啟動worker(D:\PycharmProjects\linuxTangblog)
D:\PycharmProjects> D:\PycharmProjects>cd linuxTangblog D:\PycharmProjects\linuxTangblog>celery -A linuxTangblog worker -l info -P eventlet
7在前端頁面中傳送驗證碼,並檢視日誌發現這條簡訊傳送用了2.2秒,也就是說我們已經為django節省了2.2秒:
D:\PycharmProjects>cd linuxTangblog D:\PycharmProjects\linuxTangblog>celery -A linuxTangblog worker -l info -P eventlet -------------- celery@DESKTOP-041LA6S v5.2.1 (dawn-chorus) --- ***** ----- -- ******* ---- Windows-10-10.0.19043-SP0 2021-12-15 08:43:35 - *** --- * --- - ** ---------- [config] - ** ---------- .> app: linuxTangblog:0x1c71de1ff10 - ** ---------- .> transport: redis://127.0.0.1:6379/1 - ** ---------- .> results: disabled:// - *** --- * --- .> concurrency: 8 (eventlet) -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker) --- ***** ----- -------------- [queues] .> celery exchange=celery(direct) key=celery [tasks] . user.tasks.send_sms_c [2021-12-15 08:43:35,710: WARNING/MainProcess] D:\Program Files (x86)\Python\Python310\lib\site-packages\celery\app\utils.py:204: CDeprecationWarning: The 'BROKER_URL' setting is deprecated and scheduled for removal in version 6.0.0. Use the broker_url instead deprecated.warn(description=f'The {setting!r} setting', [2021-12-15 08:43:35,712: WARNING/MainProcess] Please run `celery upgrade settings path/to/settings.py` to avoid these warnings and to allow a smoother upgrade to Celery 6.0. [2021-12-15 08:43:35,739: INFO/MainProcess] Connected to redis://127.0.0.1:6379/1 [2021-12-15 08:43:35,768: INFO/MainProcess] mingle: searching for neighbors [2021-12-15 08:43:36,833: INFO/MainProcess] mingle: all alone [2021-12-15 08:43:36,875: INFO/MainProcess] pidbox: Connected to redis://127.0.0.1:6379/1. [2021-12-15 08:43:36,890: WARNING/MainProcess] D:\Program Files (x86)\Python\Python310\lib\site-packages\celery\fixups\django.py:203: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments! warnings.warn('''Using settings.DEBUG leads to a memory [2021-12-15 08:43:36,891: INFO/MainProcess] celery@DESKTOP-041LA6S ready. [2021-12-15 08:48:50,035: INFO/MainProcess] Task user.tasks.send_sms_c[1341333a-71c5-4e18-af5f-beec3a4efaa3] received [2021-12-15 09:17:40,880: INFO/MainProcess] Task user.tasks.send_sms_c[a665e74e-73cb-4a4a-9e61-35802288d71d] received [2021-12-15 09:17:42,123: INFO/MainProcess] Task user.tasks.send_sms_c[234df19e-4254-45cb-870e-020f9ec22112] received [2021-12-15 09:17:42,149: INFO/MainProcess] Task user.tasks.send_sms_c[0b7660c0-ebe8-4ebf-938d-0538a80a69da] received [2021-12-15 09:17:42,279: INFO/MainProcess] Task user.tasks.send_sms_c[1341333a-71c5-4e18-af5f-beec3a4efaa3] succeeded in 2.2030000000086147s: '{"statusCode":"000000","templateSMS":{"smsMessageSid":"07a3d1d9c1dc4dbdbd3ba292bbc085e4","dateCreated":"20211215091742"}}'
備註:
讓當前程序在後臺執行,變成無敵,誰都幹不死它
nohup celery -A linuxTangblog worker -P gevent -c 1000 >celery.log 2>&1 &
名稱解釋:
-P gevent:celery有多種併發模式。預設情況下celery的併發是程序,但是python體系內最猛的併發是協程gevent,我們可以使用-P讓celery以gevent的方式開協程,一旦開啟後,每個程序裡面會有指定數量的協程,然後併發的給你執行任務,這樣可以提高效率
【> c e l e r y . log】:表示把日誌輸出到這個檔案裡
-c 1000:-c可以指定協程的數量,一般都設定為1000上下,這個值可能需要你結合自己的業務去測
nohup:linux命令,當前程序可以忽略所有SIGHUP結束通話訊號,也就是說無敵了,誰都幹不死它
2>&1: 把標準錯誤輸出重定向給了標準輸出,言外之意就是日誌檔案裡面既有錯誤輸出,也有正常的輸出記錄。2代表的是錯誤輸出,1代表的是標準輸出,0代表輸入,