小编Dev*_*ngh的帖子

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

我正在尝试创建一个函数(例如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 …
Run Code Online (Sandbox Code Playgroud)

python datetime schedule

5
推荐指数
1
解决办法
5557
查看次数

标签 统计

datetime ×1

python ×1

schedule ×1