如何在一段时间内(例如从上午 10 点到下午 12:30)启动/停止 Python 函数?

Dev*_*ngh 5 python datetime schedule

我正在尝试创建一个函数(例如def startTime()),该函数执行另一个函数def runFunc(),该函数每天在上午 10 点使用 python 脚本开始执行,并在下午 12:30 自动停止(或脚本结束)。

例子: startTime(start_time, stop_time,runFunc)

任何人都可以帮助我吗?

我试图安排startTime从上午 10 点到下午 12:30。

import threading
import schedule
import time

def runFunc(interval, innerFunc, iterations = 0):
   if iterations != 1:
      threading.Timer (interval,runFunc, [interval, innerFunc , 0 ]).start ()
   innerFunc ()

def A():
     print "Hello World- A"
def B():
     print "Hello World- B"
Run Code Online (Sandbox Code Playgroud)

我试过这个但没有用:

def startTime(job):
      schedule.every().day.at("10:00").do(job)
      while True:
           schedule.run_pending()

startTime(runFunc(60,A))
startTime(runFunc(300,B))
Run Code Online (Sandbox Code Playgroud)

runFunc(60,A) 运行良好,但无法在上午 10 点到下午 12:30 之间安排 runFunc。

其它的办法

from datetime import datetime, time
now = datetime.now()
now_time = now.time()
now_time
if time(5,27) <= now.time() <= time(5,28):
    runFunc(10,A)
Run Code Online (Sandbox Code Playgroud)

runFunc 确实停止,但它会在结束时间后继续执行。

Geo*_*iou 8

整个故事有点复杂,这在很大程度上取决于您真正想用脚本做什么。例如,此代码可以正常工作:

import threading
import schedule
import time
import datetime
import sys
def test():
    print('{} This is a test'.format(datetime.datetime.now())) #this works ok

def exit():
    print('{} Now the system will exit '.format(datetime.datetime.now())) #this works ok
    sys.exit()

schedule.every().day.at("09:57").do(test)
schedule.every().day.at('09:58').do(exit)

while True:
    schedule.run_pending()
    time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

您将在终端中看到“测试消息”,一分钟后您将看到实际终止脚本的“退出消息”。

但是如果你在上面的函数测试中应用一些循环,比如:

def test():
    while True: 
        print "This is a test"
        time.sleep(5)
Run Code Online (Sandbox Code Playgroud)

那么脚本将不会退出。实际上,函数 exit 甚至不会被调用,因为 Python 被函数 test 中的 while 循环所困,并且会一直继续下去。

调度文档指出调度的作业是按顺序调用的,所以如果前一个作业没有完成,下一个作业实际上并没有开始。

我怀疑您的目的是让某种功能在 10:00 持续运行,而您想在 12:30 强制停止此功能。如果不是这样,您的主要功能将在他完成工作后立即退出,您将不需要时间框架。

在这种情况下,为了解决 Python & Schedule 的序列化方式,您需要使用线程。

结合“如何并行执行作业”部分的计划文档中的信息以及来自其他答案的信息(例如如何停止正在运行的线程),此示例在我的带有 Python 2.7 的电脑中运行良好:

import threading
    import schedule
    import time
    import datetime
    import sys

def doit(stop_event, arg):
    while not stop_event.wait(1): 
        #By wait(1) you repeat the loop every 1 sec. 
        #Applying wait(0) , loops run immediatelly until to be stopped by  stop_event
        print ("working on %s" % arg)
    print("Stopping as you wish.")


def startit():
    global pill2kill
    global t
    pill2kill = threading.Event()
    t = threading.Thread(target=doit, args=(pill2kill, "task"))
    t.start()

def stopit():
    global pill2kill
    global t
    pill2kill.set()
    t.join()

#startit() #Manual call for Testing 
#time.sleep(5) #Wait 5 seconds
#stopit() #Manual call for Testing

schedule.every().day.at("12:48").do(startit)
schedule.every().day.at('12:49').do(stopit)

#schedule.every().day.at("12:50").do(startit) #Uncomment this to recall it for testing
#schedule.every().day.at('12:51').do(stopit) #Unocmment this to recall it for testing

while 1:
    schedule.run_pending()
    time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

如果满足您的需要,您还可以查看Python Crontab 库

PS:顺便说一下,通过快速查看 Python Schedule Lib 的源代码,整个故事似乎是通过捕获整个脚本并不断将 date.now() 与运行作业设置的日期进行比较来完成的。这个逻辑可以用几个默认命令和一个无限主循环来重建,以不断比较日期(就像 Schedule Lib 那样)。
这篇文章有一些不错的片段来制作你自己的 cron 作业,但只是为了测试这个简化的脚本也可以在没有外部库的情况下正常工作,当 datetime.now 在所需的开始/停止框架内时调用函数测试。

from datetime import datetime
import time

def test():
    global hasrun
    print('{} This is a test'.format(datetime.now()))
    time.sleep(5)
    hasrun=True

year,month,day,hour,minute=2016,12,23,15,55 
hasrun=False
now=datetime.now()

print "Now the time is :", now
jobstart=datetime(year,month,day,hour,minute)
jobstop=datetime(year,month, day,hour,minute+1)
print "Job will run at: ", jobstart
print "Job will finish at: ", jobstop
#print datetime.now() - jobstart
while True:
    while ((datetime.now() > jobstart) and (datetime.now() < jobstop )): 
        test()
    else:
        print('{} Please Wait...'.format(datetime.now()))
        if hasrun:
#           day=day+1
            minute=minute+2 #Just for Testing
            jobstart=datetime(year,month,day,hour,minute)
            jobstop=datetime(year,month, day,hour,minute+1)
            print "the job will run again ", jobstart
            print "and will finish at ", jobstop
            hasrun=False
        time.sleep(5)
Run Code Online (Sandbox Code Playgroud)