zoukankan      html  css  js  c++  java
  • Celery定时任务

    本文简述Celery定时任务,每天6点向指定邮箱发送笑话和一套斗图

    知识点:

    1. 简单爬虫
    2. 发送邮件
    3. Celery定时任务

    本项目 github : https://github.com/TimeAshore/Celery-QQTask

    关于Celery学习,请查看这篇文章《Python异步任务之Celery》

    项目目录结构图:

    image

    __init__.py 文件实例化Celery

    #encoding: utf-8
    # Author: Timeashore
    
    from celery import Celery
    
    cele = Celery('demo')
    cele.config_from_object('celery_app.celeryconfig')
    

    celeryconfig.py 配置Celery

    #encoding: utf-8
    # Author: Timeashore
    
    from celery.schedules import crontab
    
    # Broker and Backend
    BROKER_URL = 'redis://127.0.0.1:6379'
    CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
    
    CELERY_TIMEZONE='Asia/Shanghai'    # 指定时区,不指定默认为 'UTC'
    # CELERY_TIMEZONE='UTC'
    
    # import
    CELERY_IMPORTS = (
        'celery_app.task1',
    )
    # schedules
    CELERYBEAT_SCHEDULE = {
        'multiply-at-some-time': {
            'task': 'celery_app.task1.send_email_image',
            'schedule': crontab(hour=6, minute=20),   # 每天早上 6 点 00 分执行一次
            'args': ()                                  # 任务函数参数
        }
    }
    

    task1.py 是定时任务文件,发送图片邮件部分代码:

    content = MIMEText(con + '''<html><body><table>
                                  <tr>
                                        <td><img src="cid:imageid1"></td><td><img src="cid:imageid3"></td>
                                   </tr>
                                  <tr>
                                        <td><img src="cid:imageid2"></td><td><img src="cid:imageid4"></td>
                                  </tr>
                                  <tr>
                                        <td><img src="cid:imageid5"></td><td><img src="cid:imageid7"></td>
                                   </tr>
                                  <tr>
                                        <td><img src="cid:imageid6"></td><td><img src="cid:imageid8"></td>
                                  </tr>
                            </table></body></html>''', 'html', 'utf-8')
        msg.attach(content)
        for x in range(1, 9):
            with open("{}.{}".format(x, point), "rb") as f:
                img_data = f.read()
            img = MIMEImage(img_data)
            img.add_header('Content-ID', 'imageid{}'.format(str(x)))
            msg.attach(img)
        try:
            s = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 邮件服务器及端口号
            s.login(msg_from, passwd)
            s.sendmail(msg_from, 收件人, msg.as_string())
            print u"发送成功"
        except Exception, e:
            print u"发送失败", e.message
        finally:
            s.quit()
    

    邮件使用 QQ 邮箱,前提需要开启账户 POP3 和 IMAP , 具体设置请看项目代码。

    执行

    1,在celery_app同级目录下,启动Celery worker 进程

    celery -A celery_app worker --loglevel=info
    

    启动成功如下图:

    image
    2,在celery_app同级目录下,开启定时任务,周期性的把 task 发送到 Broker

    celery beat -A celery_app
    

    如下图:

    image

    至此,部署到服务器。
    完。

    展示图:

    image

    同样的,按照这个思路,可以很简单的实现天气预报实时通知等。

    任务执行时间

    crontab(minute='*/1')  # 每分钟
    crontab(hour=6, minute=20)  # 每天6::20
    crontab(minute='30', hour='17', day_of_week='5')  # 每周五17:30
    crontab(minute=0, hour=0, day_of_month='5')  # 每月5号0:00
    
  • 相关阅读:
    Linux上vi(vim)编辑器使用教程
    什么是servlet
    探索ArrayList自动改变size真相
    十种算法
    二级缓存:EHCache的使用
    Lucene:基于Java的全文检索引擎简介
    简述 Hibernate 和 JDBC 的区别、优缺点
    Spring学习笔记
    如何复制表结构、如何复制表数据:
    破解安装 SecureCRT 7.0.2 for mac完美破解版,mac secureCRT , apple secureCRT
  • 原文地址:https://www.cnblogs.com/ldy-miss/p/9141850.html
Copyright © 2011-2022 走看看