11: python中的轻量级定时任务调度库:schedule
2024-10-11 01:49:58
1.1 schedule 基本使用
1、schedule 介绍
1. 提到定时任务调度的时候,相信很多人会想到芹菜celery,要么就写个脚本塞到crontab中。
2. 不过,一个小的定时脚本,要用celery的话太“重”了。所以,我找到了一个轻量级的定时任务调度的库:schedule。
3. 安装: pip install schedule
2、schedule最基本使用
官网地址:https://schedule.readthedocs.io/en/stable/
参考博客:https://www.cnblogs.com/anpengapple/p/8051923.html
#! -*- coding:utf8 -*-
import schedule
import time def job():
print("I'm working...") schedule.every(1).seconds.do(job)
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job) while True:
schedule.run_pending()
time.sleep(1)
schedule最简单使用
1. 这是在pypi上面给出的示例,通过这个栗子,我们也可以知道,schedule其实就只是个定时器。
2. 在while True死循环中,schedule.run_pending()是保持schedule一直运行,去查询上面那一堆的任务,在任务中,就可以设置不同的时间去运行,跟crontab是类似的。
3. 但是,如果是多个任务运行的话,实际上它们是按照顺序从上往下挨个执行的。如果上面的任务比较复杂,会影响到下面任务的运行时间
3、利用python 用多线程/多进程 结合 schedule 实现异步定时任务
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import schedule
import threading
import time def job1():
print("I'm working for job1")
time.sleep(2)
print("job1:", datetime.datetime.now()) def job2():
print("I'm working for job2")
time.sleep(2)
print("job2:", datetime.datetime.now()) def job1_task():
threading.Thread(target=job1).start() def job2_task():
threading.Thread(target=job2).start() def run():
schedule.every(1).seconds.do(job1_task)
schedule.every(1).seconds.do(job2_task) while True:
schedule.run_pending()
time.sleep(1)
run()
利用python 用多线程/多进程 结合 schedule 实现异步定时任务
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import schedule
import threading
import time from lib.logs import Logger
from backends.syn_ldap_to_db import store_ldap_to_db def syn_ldap_info():
threading.Thread(target=store_ldap_to_db).start() def crond_pre_employee():
schedule.every().day.at("13:40").do(syn_ldap_info)
# schedule.every(1).seconds.do(syn_ldap_info)
# schedule.every(1).seconds.do(job2_task)
while True:
schedule.run_pending()
time.sleep(1) def run_cround():
print '########################### run crond tasks ##############################'
now = time.strftime('%Y-%m-%d %H:%M:%S')
msg = 'pre_staff中定时任务开始监控,时间【%s】' %now
Logger().log(msg, True)
threading.Thread(target=crond_pre_employee).start() run_cround()
定时任务运用
最新文章
- Servlet转码问题
- SharePoint 2013 修改表单认证登录页面
- 改变Vim在iTerm2中的光标
- SSN 社会安全号码
- smarty模板中引用常量没效果
- Android APP高效开发的十大建议
- java 正则匹配空格字符串 正则表达式截取字符串
- EF架构~过滤导航属性等,拼接SQL字符串
- UITabBarController自定义二之xib
- 第三章 Struts2配置详解
- python二维码生成器
- int float double 最小值与最大值
- grunt 插件
- Linux二进制分析PDF
- X86架构
- myEclipse全局搜索时报错
- Python3用gevent写个文件字符串查找器
- git知识点
- Drupal7配置简洁URL(Clear URL)
- 爬虫学习之-操作mysql
热门文章
- 动手动脑(&;课后实验):生成随机数,函数的重载
- PHP 函数 ignore_user_abort()详解笔记
- EL的隐含对象 (二)【访问作用域范围的隐含对象】
- .NET 黑魔法 - asp.net core 自定义格式的响应
- 在caffe-ssd的环境搭建中遇到报错信息:Makefile:588: recipe for target '.build_release/cuda/src/caffe/layers/softmax_loss_layer.o' failed
- [ English ] 俚语 “Ping me=打我电话”
- python3 TypeError: a bytes-like object is required, not 'str'
- 《大话设计模式》c++实现 原型模式
- Python全栈-day11-函数3
- html5-文件的基本格式