您好,登錄后才能下訂單哦!
1、配置文件
vi setting.py
#coding:utf-8 import?os from?datetime?import??timedelta 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'?#?讀取任務結果一般性能要求不高,所以使用了可讀性更好的JSON # CELERY_TASK_RESULT_EXPIRES?=?60?*?60?*?24?#?任務過期時間,不建議直接寫86400,應該讓這樣的magic數字表述更明顯 CELERY_ACCEPT_CONTENT?=?['json']?#?指定接受的內容類型 CELERYBEAT_SCHEDULE?=?{ ????'send_mail':?{ ????????'task':?'celery_test.tasks.send_mail', ???????'schedule':?timedelta(seconds=10), ????} }
2、函數模塊
vi tasks.py
#coding:utf-8 from?server?import?app import?random,string,smtplib @app.task def?send_mail(): ????????SUBJECT="臨時登錄密碼" ????????HOST="smtp.163.com" ????????#?TO=mail ????????TO='456@qq.com' ????????FROM="123@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("123@qq.com","第三方發送驗證碼") ????????server.sendmail(FROM,[TO],BODY) ????????server.quit() ????????return?True
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、啟動worker,執行任務
celery -A celery_test.server worker -l info
5、啟動beat,生成任務
celery -A celery_test.server beat
6、目錄結構
celery_test/{server.py,setting.py,tasks.py}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。