其他分享
首页 > 其他分享> > apscheduler定时器

apscheduler定时器

作者:互联网

apscheduler定时模块

安装

pip install apscheduler -i https://pypi.tuna.tsinghua.edu.cn/simple

定时获取数据库数据demo

import pymysql.cursors
from datetime import datetime
import os
from apscheduler.schedulers.blocking import BlockingScheduler


def func(*args,**kwargs):
    global cursor
    try:
        # 连接数据库
        connect = pymysql.Connect(
            host="127.0.0.1",
            port=3306,
            user="root",
            password="project",
            db="blog_project",
            charset="utf8"
        )
        cursor = connect.cursor(cursor=pymysql.cursors.DictCursor)
        print('连接成功')
    except Exception as e:
        return str(e)
    try:
        sql = "select id from blog_staff where read_number >= 50"
        cursor.execute(sql)
        message_list = cursor.fetchall()
        print('查询中'+ str(datetime.now()))
        cursor.close()
        connect.close()
        print('连接关闭')
        return message_list
    except Exception as e:
        return ster(e)

def run():

    print(func())

if __name__ == '__main__':
    # 实例化
    scheduler = BlockingScheduler()
    scheduler.add_job(run, 'cron',minute="*/1")
    # 执行
    try:
        scheduler.start()
    except Exception as e:
        print(e)
        
"""
    crontab()    每分钟

    crontab(minute=0, hour=0)    每天的0时0分

    crontab(minute=0,hour='*/3')   每三个小时

    crontab(day_of_week='sunday')   每周日的每一小时

    crontab(minute=0,hour='/3,8-17')   8时到17时的每三个小时
"""

标签:__,定时器,crontab,cursor,apscheduler,print,minute
来源: https://blog.csdn.net/asddddddddsg/article/details/120273075