- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建一个函数(例如 def startTime()
)来执行另一个函数,例如 def runFunc()
,该函数每天在 python 脚本执行时开始于上午 10 点,并在中午 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"
我试过了但是没用:
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
) 运行良好,但它无法将 runFunc 安排在上午 10 点到下午 12:30 之间。
另一种方式
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:00持续运行,你想在12:30强制停止这个功能。如果不是这样,您的主要功能将在他完成工作后立即退出,您不需要时间框架。
在这种情况下,为了解决 Python 和 Schedule 的序列化方式,您需要使用线程。
结合 "how to execute jobs in parallel" 上的计划文档中的信息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)
您还可以查看 Python Crontab library如果适合您的需要。
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)
关于python - 如何在一段时间内启动/停止 Python 函数(例如从上午 10 点到 12 点 :30pm)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41289947/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!