1. Configuration files
VI setting.py
#coding: Utf-8import osfrom datetime Import timedeltabroker_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 = Truecelery_task_serializer = ' JSON ' # Task serialization and deserialization using the Msgpack scheme #celery_result_serializer = ' JSON ' # Read task Results General performance requirements are not high, so use a more readable json#celery_task_result_ EXPIRES = 60 * 60 * 24 # Task Expiration time, not recommended to write directly 86400, should let such magic number expression more obvious celery_accept_content = [' JSON '] # Specifies the accepted content type celerybeat_sched ULE = {' Send_mail ': {' task ': ' Celery_test.tasks.send_mail ', ' Schedule ': Timedelta (seconds=10),}}
2. Function module
VI tasks.py
#coding: Utf-8from server import appimport random,string,smtplib@app.taskdef send_mail (): subject= "Temporary login password" host= "Smtp.163.com" # TO=mail to= ' [email protected] ' from= "[email protected]" 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 ("[EmAil protected] "," third party Send Verification Code ") server.sendmail (FROM,[TO],BODY ) server.quit () Return true
3. Starting module
VI server.py
From celery import celeryapp=celery (' Celery_test ', include=[' celery_test.tasks ']) app.config_from_object (' Celery_ Test.setting ') if __name__== ' __main__ ': App.start ()
4. Start the worker and perform the task
Celery-a Celery_test.server worker-l Info
5. Start beat, generate tasks
Celery-a Celery_test.server Beat
6. Directory Structure
celery_test/{server.py,setting.py,tasks.py}
Celery example implementation of sending messages on a timed basis