Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计划程序在某些时间内运行某些任务无法正常工作_Python_Datetime_Python 2.7_Scheduled Tasks_Scheduler - Fatal编程技术网

Python 计划程序在某些时间内运行某些任务无法正常工作

Python 计划程序在某些时间内运行某些任务无法正常工作,python,datetime,python-2.7,scheduled-tasks,scheduler,Python,Datetime,Python 2.7,Scheduled Tasks,Scheduler,我正在尝试创建一个调度程序,以便在特定时间运行脚本 下面的代码是我自己的版本,但当时间到达23:59:59,并跨越到00:00:01时出现问题,因为某些原因,它不会继续空转。。。相反,它会调用callscripts()一次,然后返回到空闲状态 from datetime import date, timedelta from sched import scheduler from time import time, sleep, strftime import random s = sched

我正在尝试创建一个调度程序,以便在特定时间运行脚本

下面的代码是我自己的版本,但当时间到达23:59:59,并跨越到00:00:01时出现问题,因为某些原因,它不会继续空转。。。相反,它会调用callscripts()一次,然后返回到空闲状态

from datetime import date, timedelta
from sched import scheduler
from time import time, sleep, strftime
import random

s = scheduler(time, sleep)
random.seed()

def periodically(runtime, intsmall, intlarge, function):
     ## Get current time
    currenttime = strftime('%H:%M:%S')

    ## If currenttime is anywhere between 23:40 and 23:50 then...
    if currenttime > '23:40:00' and currenttime < '23:50:00':
        ## Call clear
        clear()
        ## Update time
        currenttime = strftime('%H:%M:%S')

    ## Idle time
    while currenttime > '23:40:00' and currenttime < '23:59:59' or currenttime > '00:00:00' and currenttime < '01:30:00':
        ## Update time
        currenttime = strftime('%H:%M:%S')

    runtime += random.randrange(intsmall, intlarge)
    s.enter(runtime, 1, function, ())
    s.run()

def callscripts():
    print "Calling Functions"
    main1()
    main2()

def main1():
    print "Main1"

def main2():
    print "Main2"

def clear():
    print "Clearing"

while True:
    periodically(2, -1, +1, callscripts)
from datetime导入日期,timedelta
从sched导入计划程序
从时间导入时间、睡眠、strftime
随机输入
s=调度程序(时间、睡眠)
random.seed()
定期定义(运行时、intsmall、intlarge、函数):
##获取当前时间
currenttime=strftime(“%H:%M:%S”)
##如果currenttime介于23:40和23:50之间,则。。。
如果当前时间>23:40:00且当前时间<'23:50:00':
##清除
清除()
##更新时间
currenttime=strftime(“%H:%M:%S”)
##空闲时间
当currenttime>'23:40:00'和currenttime<'23:59:59'或currenttime>'00:00:00'和currenttime<'01:30:00'时:
##更新时间
currenttime=strftime(“%H:%M:%S”)
运行时+=random.randrange(intsmall,intlarge)
s、 输入(运行时,1,函数,())
s、 运行()
def callscripts():
打印“调用函数”
main 1()
main 2()
def main1():
打印“Main1”
def main 2():
打印“Main2”
def clear():
打印“清算”
尽管如此:
定期(2,-1,+1,调用脚本)
有人知道如何解决这个问题,或者知道更好的方法吗


非常感谢AEA

这只是一个关于如何比使用字符串更有效地处理事情的概念

import time, calendar

# Get the current time in a localtime manner (we need it for the day and year)
today = time.localtime(time.time())
# We put the day, month and year into a strftime -> localtime converter
# giving us the localtime version of 00:00:00 of today (there's cleaner version for this process)
today_strftime = time.strptime(str(today.tm_mday) + " " + time.strftime("%b") + " " + str(today.tm_year)[-2:], "%d %b %y")
# Then we take that localtime of 00:00:00 and put it into timegm which gives us the unix
# timestamp of 00:00:00 today, which we can subtract from time.time() to give us how many
# seconds of today has passed since 00:00:00.. Which we can use to compare integers with eachother (more efficient, and easy to compare)
unixtime_beginning_of_day = calendar.timegm(today_strftime)
time_passed_today = time.time() - unixtime_beginning_of_day

if time_passed_today > 3600:
    # 60sec per min * 60 minutes per hour == 3600 seconds in one hour.
    print "One hour has passed since the start of this day"

应该是currenttime>='00:00:00'为什么不利用
localtime()
或者仅仅利用
time()
更多?这将如何实现?有没有可能有一个有效的例子?注意:这不是一个干净的版本,这是一个可行的概念,但它不是有效的或性感的观看。