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()

定时任务运用

最新文章

  1. Servlet转码问题
  2. SharePoint 2013 修改表单认证登录页面
  3. 改变Vim在iTerm2中的光标
  4. SSN 社会安全号码
  5. smarty模板中引用常量没效果
  6. Android APP高效开发的十大建议
  7. java 正则匹配空格字符串 正则表达式截取字符串
  8. EF架构~过滤导航属性等,拼接SQL字符串
  9. UITabBarController自定义二之xib
  10. 第三章 Struts2配置详解
  11. python二维码生成器
  12. int float double 最小值与最大值
  13. grunt 插件
  14. Linux二进制分析PDF
  15. X86架构
  16. myEclipse全局搜索时报错
  17. Python3用gevent写个文件字符串查找器
  18. git知识点
  19. Drupal7配置简洁URL(Clear URL)
  20. 爬虫学习之-操作mysql

热门文章

  1. 动手动脑(&课后实验):生成随机数,函数的重载
  2. PHP 函数 ignore_user_abort()详解笔记
  3. EL的隐含对象 (二)【访问作用域范围的隐含对象】
  4. .NET 黑魔法 - asp.net core 自定义格式的响应
  5. 在caffe-ssd的环境搭建中遇到报错信息:Makefile:588: recipe for target '.build_release/cuda/src/caffe/layers/softmax_loss_layer.o' failed
  6. [ English ] 俚语 “Ping me=打我电话”
  7. python3 TypeError: a bytes-like object is required, not 'str'
  8. 《大话设计模式》c++实现 原型模式
  9. Python全栈-day11-函数3
  10. html5-文件的基本格式