模块 schedule 定时任务

schedule模块实现定时任务

2018-08-29 15:01:51 更多

一、官方示例

import schedule
import time 

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).days.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job) 

while True:
    schedule.run_pending()
    time.sleep(1)

二、多线程解决多任务串行执行任务的延迟问题

未使用多线程

import datetime
import schedule
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 run():
    schedule.every(10).seconds.do(job1)
    schedule.every(10).seconds.do(job2)     

while True:
    schedule.run_pending()
    time.sleep(1)

使用多线程

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(10).seconds.do(job1_task)
    schedule.every(10).seconds.do(job2_task)

    while True:
        schedule.run_pending()
        time.sleep(1)

原文地址:https://www.cnblogs.com/ham-731/p/12121482.html

时间: 2024-08-30 13:23:45

模块 schedule 定时任务的相关文章

python用schedule模块实现定时任务

schedule模块用法 1 import schedule 2 import time 3 4 def test(): 5 print("I'm working...") 6 def test2(): 7     print("I'm working... in job2") 8 9 # 每10分钟执行一次job函数 10 schedule.every(10).minutes.do(test) 11 # 每10秒执行一次job函数 12 schedule.ever

定时任务模块 schedule

# coding:utf-8 from learning_python.Telegram_push.check_hardware import check_cpu import schedule import time schedule.every(5).seconds.do(check_cpu) while True: schedule.run_pending() time.sleep(1) 原文地址:https://www.cnblogs.com/yangxiufeng/p/10677680

spring schedule定时任务(一):注解的方式

我所知道的java定时任务的几种常用方式: 1.spring schedule注解的方式: 2.spring schedule配置文件的方式: 3.java类继承TimerTask: 第一种方式的实现: 1.使用maven创建spring项目,schedule在spring-context.jar的包下边,因此需要导入与之相关的包:同时,我配的是spring web项目,也同时导入了spring-web和spring-webmvc的包,如下: <dependency> <groupId&

spring boot项目中处理Schedule定时任务

初始化之后,我们在spring boot的入口类Application.java中,允许支持schedule @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 然后,新建一个执行类Jobs.java @Component pub

spring schedule定时任务(二):配置文件的方式

接着上一篇,这里使用spring配置文件的方式生成spring定时任务. 1.相应的web.xml没有什么变化,因此便不再罗列.同样的,相应的java代码业务逻辑改动也不大,只是在原来的基础上去掉@Component和@Scheduled(cron = "0/5 * * * * ?")参数,也就是把这个类和方法变成一个最简单的java类和方法就可以了. 2.既然是配置文件的方式,那么改动大的自然就是pring.xml配置,把原本用注解实现的定时功能放到配置中来,改动后的配置如下: &l

spring schedule 定时任务

1.主方法的类或者需要执行定时任务的类加上@EnableScheduling注解 2.定时任务的方法加上@Scheduled注解,并加上时间设置 3.定时任务时间设置有两种,一种是用fixedRate,一种是con表达式 3.1fixedRate用法如: @Scheduled(fixedRate = 1000 * 60) fixedRate的value是long类型的毫秒数: 这种形式的定时任务不够灵活,不能根据年月日等设置. 3.2con表达式如: cron表达式详解 Cron表达式是一个字符

Spring Schedule定时任务多线程执行任务配置

@Configuration public class ScheduleConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { Method[] methods = BatchProperties.Job.class.getMethods(); int defaultPoolSize = 10; int corePoo

ansible安装部署和配置、常用模块整理

今天心情不错~~~~第25个生日了,又遇昨晚百年难得一见的蓝月亮,所以昨晚连夜整理了文档, 会分为两部分发出去,ansible批量化部署在工作中是非常实用,建议呢 整理大量常用模块去练习 1.1.1 ansible软件介绍 1. ansible是一个基于python开发的自动化运维工具!(saltstack) 2. 其功能的实现是基础SSH远程连接服务的 3. ansible可以实现批量系统配置.批量软件部署.批量文件拷贝.批量运行命令等功能 1.1.2 ansible软件相关参考链接信息 ht

快速构建Python爬虫IP代理池服务

在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳定的运行,当然在公司做的东西不能开源出来.不过呢,闲暇时间手痒,所以就想利用一些免费的资源搞一个简单的代理池服务. 1.问题 代理IP从何而来? 刚自学爬虫的时候没有代理IP就去西刺.快代理之类有免费代理的网站去爬,还是有个别代理能用.当然,如果你有更好的代理接口也可以自己接入. 免费代理的采集也很简单,无非就是:访问页面页面 —> 正则/xpath提取