1、vi tasks.py
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站建設、網(wǎng)站建設、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務鹿泉,10年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18982081108#coding:utf-8 from server import app import random,string,smtplib @app.task def add(x,y): return x+y @app.task def send_mail(): SUBJECT="臨時登錄密碼" HOST="smtp.163.com" # TO=passwords['config']['email'] TO='1235@qq.com' FROM="456@163.com" text=str(random.randint(1000,9999)) BODY=string.join(( "From:%s"%FROM, "To:%s"%TO, "Subject:%s"%SUBJECT, "",text),"\r\n") server=smtplib.SMTP(HOST) server.login("456@163.com","第三方發(fā)送驗證碼") server.sendmail(FROM,[TO],BODY) server.quit()
2、vi setting.py
#coding:utf-8 from datetime import timedelta from kombu import Exchange, Queue BROKER_URL = 'redis://127.0.0.1:6379/13' CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/14' CELERY_TIMEZONE = 'Asia/Shanghai' CELERY_ENABLE_UTC = True CELERY_TASK_SERIALIZER = 'json' # 任務序列化和反序列化使用msgpack方案 CELERY_RESULT_SERIALIZER = 'json' # 讀取任務結(jié)果一般性能要求不高,所以使用了可讀性更好的JSON # CELERY_TASK_RESULT_EXPIRES = 60 * 60 * 24 # 任務過期時間,不建議直接寫86400,應該讓這樣的magic數(shù)字表述更明顯 CELERY_ACCEPT_CONTENT = ['json'] # 指定接受的內(nèi)容類型 CELERY_QUEUES = ( Queue('default', Exchange('default'), routing_key='default'), Queue('for_add', Exchange('for_task_add'), routing_key='for_task_add'), Queue('for_send_email', Exchange('for_task_email'), routing_key='for_task_email'), ) CELERY_ROUTES = { 'celery_test.tasks.add': {'queue': 'for_add', 'routing_key': 'for_task_add'}, 'celery_test.tasks.send_mail': {'queue': 'for_send_email', 'routing_key': 'for_task_email'}, } CELERYBEAT_SCHEDULE = { 'send_mail': { 'task': 'celery_test.tasks.send_mail', 'schedule': timedelta(seconds=30), } , 'add': { 'task': 'celery_test.tasks.add', 'schedule': timedelta(seconds=10), 'args':(16,16) } }
3、vi server.py
from celery import Celery app=Celery('celery_test',include=['celery_test.tasks']) app.config_from_object('celery_test.setting') if __name__=='__main__': app.start()
4、啟動
celery -A celery_test.server worker -l info -Q for_send_email
celery -A celery_test.server worker -l info -Q for_add
celery -A celery_test.server beat
5、通過flower監(jiān)控
pip install flower
celery flower --port=5555 --broker=redis://localhost:6379/0
6、訪問
127.0.0.1:5555
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。