python定时器-python定时器

1、指定时间,每天定时运行

def func():

main_fun() ##主要的函数

#如果需要循环调用,就要添加以下方法

timer = threading.Timer(86400, func)

#timer = threading.Timer(180, func)

timer.start()

if __name__ == "__main__":

spmcs_conn = cx_Oracle.connect("%s","nbadv","%s",encoding = "UTF-8",nencoding = "UTF-8")

cur = spmcs_conn.cursor()

now_time = datetime.datetime.now()

now_day = now_time.date().day

# 获取明天时间

next_time = now_time + datetime.timedelta(days=+1)

next_year = next_time.date().year

next_month = next_time.date().month

next_day = next_time.date().day

# 获取明天3点时间

#next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 03:00:00", "%Y-%m-%d %H:%M:%S")

next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 07:00:00", "%Y-%m-%d %H:%M:%S")

# # 获取昨天时间

# last_time = now_time + datetime.timedelta(days=-1)

# 获取距离明天3点时间,单位为秒

timer_start_time = (next_time - now_time).total_seconds()

#print(timer_start_time)

# 54186.75975

#if timer_start_time >= 0:

#定时器,参数为(多少时间后执行,单位为秒,执行的方法)

timer = threading.Timer(timer_start_time, func)

timer.start()

2、隔一定时间运行程序

import datetime

import time

def doSth():

# 把爬虫程序放在这个类里

print(u"这个程序要开始疯狂的运转啦")

# 一般网站都是1:00点更新数据,所以每天凌晨一点启动

def main(h=1,m=0):

while True:

now = datetime.datetime.now()

# print(now.hour, now.minute)

if now.hour == h and now.minute == m:

doSth()

# 每隔60秒检测一次

time.sleep(60)

main()

import time

from apscheduler.schedulers.background import BackgroundScheduler

def job():

print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))

if __name__ == "__main__":

# BackgroundScheduler: 适合于要求任何在程序后台运行的情况,当希望调度器在应用后台执行时使用

scheduler = BackgroundScheduler()

# 采用非阻塞的方式

# 采用corn的方式

scheduler.add_job(job, "cron", day_of_week="fri", second="*/5")

"""

year (int|str) – 4-digit year

month (int|str) – month (1-12)

day (int|str) – day of the (1-31)

week (int|str) – ISO week (1-53)

day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)

hour (int|str) – hour (0-23)

minute (int|str) – minute (0-59)

econd (int|str) – second (0-59)

start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)

end_date (datetime|str) – latest possible date/time to trigger on (inclusive)

timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)

* any Fire on every value

*/a any Fire every a values, starting from the minimum

a-b any Fire on any value within the a-b range (a must be smaller than b)

a-b/c any Fire every c values within the a-b range

xth y day Fire on the x -th occurrence of weekday y within the month

last x day Fire on the last occurrence of weekday x within the month

last day Fire on the last day within the month

x,y,z any Fire on any matching expression; can combine any number of any of the above expressions

"""

scheduler.start()

# 其他任务是独立的线程

while True:

print("main-start:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))

time.sleep(2)

print("main-end:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))

##https://www.jianshu.com/p/b77d934cc252

上面的链接里讲述了很多定时器

from apscheduler.schedulers.blocking import BlockingScheduler

from datetime import datetime

def job():

print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

scheduler = BlockingScheduler()

scheduler.add_job(job,"cron",hour ="8",minute= "59")

scheduler.start()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值