python上午下午问题_如何从上午10点到下午12点30分启动/停止python功能

我正在尝试创建函数(例如def startTime():),它执行另一个函数,比如def runFunc(),每天上午10点用

python脚本开始执行,并在12:30 pm自动停止(或脚本结束).

喜欢

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"

尝试但没有奏效

def startTime(job):

schedule.every().day.at("10:00").do(job)

while True:

schedule.run_pending()

startTime(runFunc(60,A))

startTime(runFunc(300,B))

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)

runFunc会停止,它会在一段时间后继续执行

最佳答案 整个故事有点复杂,它在很大程度上取决于你真正想要的脚本.

例如,此代码可以正常工作:

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)

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

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

def test():

while True:

print "This is a test"

time.sleep(5)

然后脚本不会退出.

实际上,函数exit甚至不会被调用,因为Python被函数测试中的while循环所困,并且会继续运行.

计划文档指出计划作业是按顺序调用的,因此如果上一个作业未完成,则下一个作业实际上不会启动.

我怀疑你的目的是让一种功能在10点连续运行,你想在12:30强制停止这个功能.如果不是这样,你的主要功能将在他完成工作后立即退出,你不需要时间框架.

在这种情况下,为了解决Python&的序列化方式安排你需要使用线程.

结合来自“how to execute jobs in parallel”部分的Schedule Documentation和来自Overflow的其他答案的信息,如how to stop a running thread,这个例子在我的电脑上使用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)

PS:顺便说一句,通过快速查看Python Schedule Lib的源代码,似乎整个故事是通过捕获整个脚本并不断比较date.now()和日期设置来运行作业来完成的.这个逻辑可以用几个默认命令和一个无限主循环来重建,以便不断地比较日期(比如Schedule Lib).

This post有一些很好的片段来制作你自己的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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值