Celery example implementation of sending messages on a timed basis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.