python每天定时9点执行_python定时执行方法

python每天定时9点执行_python定时执行方法1time.sleepimporttimeforiinrange(5):print(i)time.sleep(10)2用shedimporttimeimportschedschedule=sched.scheduler(time.time,time.sleep)deffunc(string1,float1):print(“nowis”,time.time(),”…

大家好,又见面了,我是你们的朋友全栈君。

1 time.sleep

import time

for i in range(5):

print(i)

time.sleep(10)

2 用shed

copycode.gif

import time

import sched

schedule = sched.scheduler ( time.time, time.sleep )

def func(string1,float1):

print(“now is”,time.time(),” | output=”,string1,float1)

print(time.time())

schedule.enter(2,0,func,(“test1”,time.time()))

schedule.enter(2,0,func,(“test1”,time.time()))

schedule.enter(3,0,func,(“test1”,time.time()))

schedule.enter(4,0,func,(“test1”,time.time()))

schedule.run()

print(time.time())

copycode.gif

其中func中放要执行的函数,用schedule.enter加入要执行的函数,里面的第一个参数是延迟执行的时间,用sched.scheduler进行初始化

copycode.gif

1512033155.9311035

now is 1512033157.9316308 | output= test1 1512033155.9311035

now is 1512033157.9316308 | output= test1 1512033155.9311035

now is 1512033158.9322016 | output= test1 1512033155.9311035

now is 1512033159.9316351 | output= test1 1512033155.9311035

1512033159.9316351

[Finished in 4.2s]

copycode.gif

上面是执行结果,缺点是任务队列是阻塞型,即schedule里的任务不执行完,后面的主线程就不会执行

3 用threading里的timer,实现非阻塞型,即主线程要任务同时执行

copycode.gif

import time

from threading import Timer

def print_time( enter_time ):

print “now is”, time.time() , “enter_the_box_time is”, enter_time

print time.time()

Timer(5, print_time, ( time.time(), )).start()

Timer(10, print_time, ( time.time(), )).start()

print time.time()

copycode.gif

执行结果:

1512034286.9443169

1512034286.9452875

now is 1512034291.9460146 enter_the_box_time is 1512034286.9443169

now is 1512034296.9461012 enter_the_box_time is 1512034286.9452875

[Finished in 10.2s]

可看出任务和主线程是同步执行,但是后3位又稍有不同,应该是python的多线程并非真正的多线程导致

每天某个时间定时执行任务:

copycode.gif

import datetime

import time

def doSth():

print(‘test’)

# 假装做这件事情需要一分钟

time.sleep(60)

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

”’h表示设定的小时,m为设定的分钟”’

while True:

# 判断是否达到设定时间,例如0:00

while True:

now = datetime.datetime.now()

# 到达设定时间,结束内循环

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

break

# 不到时间就等20秒之后再次检测

time.sleep(20)

# 做正事,一天做一次

doSth()

main()

copycode.gif

4 linux用 crontab

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160693.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JSP、JavaBean原理和使用

    JSP、JavaBean原理和使用JavaServerPages:Java服务器端页面,也和Servlet一样,用于动态Web技术!最大的特点:写JSP就像在写HTML区别:HTML只给用户提供静态的数据 JSP页面中可以嵌入JAVA代码,为用户提供动态数据;…

    2022年7月12日
    20
  • datagrip 2021 激活码_最新在线免费激活

    (datagrip 2021 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~FNWC…

    2022年3月30日
    221
  • goland 2021.01激活码【永久激活】

    (goland 2021.01激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~B…

    2022年3月22日
    273
  • py文件的运行

    安装过程及配置安装过程及配置安装过程及配置安装过程准备:下载好Python的安装程序后,开始安装,在进入安装界面后一定确保勾选将Python加入到系统环境变量的路径里。如图所示:2如果没有

    2022年3月29日
    79
  • 决策树模型参数释义「建议收藏」

    决策树模型参数释义「建议收藏」转自https://blog.csdn.net/qq_16000815/article/details/80954039”’scikit-learn中有两类决策树,它们均采用优化的CART决策树算法。”’fromsklearn.treeimportDecisionTreeRegressor”’回归决策树”’DecisionTreeRegressor(criterio…

    2022年10月8日
    5
  • c语言位运算符的用法_c语言运算符大全

    c语言位运算符的用法_c语言运算符大全一、位运算符C语言提供了六种位运算符:&按位与|按位或^按位异或~取反>>右移1.按位与运算按位与运算符”&”是双目运算符。其功能是参与运算的两数各对应的二进位相与。只有对应的两个二进位均为1时,结果位才为1,否则为0。参与运算的数

    2022年10月5日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号