【问题标题】:Python sched scheduler and rebootsPython sched 调度程序和重启
【发布时间】:2011-06-01 09:06:14
【问题描述】:

我已经阅读了有关 python sched(任务调度程序)的信息,它的工作原理类似于 cron。

但我有一个问题:

  • 假设我安排一个函数在每 2 小时后运行一次,同时我的系统关闭,然后我再次重新启动系统...

调度程序是否会在 2 小时后自动启动并运行该功能?还是必须在关闭系统后重新启动?

  • sched 是否像守护进程一样工作?

【问题讨论】:

  • 您是否阅读了sched 的来源?这是 135 行代码。哪一部分难以理解?
  • 我是新手,所以无法理解。我想问的是 turbogear 说它是 asmini cron her :razorvine.net/download/kronos.py。但它使用sched。那怎么会是cron呢?

标签: python scheduled-tasks


【解决方案1】:

如果是这样的话:
即使在系统重新启动后这也能工作吗?
答案是:不,那么 turbogear 调度程序如何在 cron 中使用 cronos 运行?在 SYSTEM 重启后,turbogear 中的预定事件也将消失。
如果我错了,请纠正我。

import time
import sched
import datetime
import threading
import calendar
#from datetime import datetime


class test:

    def __init__(self):
        self.name = ''

    def getSec(self):

        now = datetime.datetime.now()
        print "now - ", now
        currentYear = now.year
        currentMonth = now.month
        currentDay = now.day
        currentHour = now.hour
        currentMinute = now.minute
        currentSecond = now.second
        currentMicroseconds = now.microsecond
        command = "python runbackup.py"
        print "command is - ", command

        print "currentMinute - ", currentMinute
        print "currentSecond - ", currentSecond
        # current time
        a = datetime.datetime(currentYear, currentMonth, currentDay, currentHour, currentMinute, currentSecond, currentMicroseconds)

        last_date_of_current_month = calendar.monthrange(currentYear, currentMonth)[1]
        print "last_date_of_current_month - ", last_date_of_current_month
        b = datetime.datetime(currentYear, currentMonth, int(last_date_of_current_month), 23, 59, 59, 000000)
        #b = datetime.datetime(currentYear, currentMonth, int(29), 18, 29, 00, 000000)
        #print "date time of b is - %s %s " % (18, 29)

        c = b-a
        print "c is - ", c

        time.sleep(1)
        scheduler = sched.scheduler(time.time, time.sleep)
        #scheduler.cancel(e1)
        sec = c.seconds
        print "second -  ", sec
        print "scheduler entered."
        e1 = scheduler.enter(sec, 1, self.getSec, ())
        t = threading.Thread(target=scheduler.run)
        print "thread started."
        print "======================================"
        t.start()

        #scheduler.cancel(e1)
        #print "canceled."

        return True

if __name__=='__main__'  :
    obj = test()
    obj.getSec()

【讨论】:

    【解决方案2】:

    三个问题的答案都是否。

    sched 与 cron 不同。它需要一个通用计时器或计数器函数和一个延迟函数,并允许您在特定时间(由通用计时器函数定义的事件)之后安排函数调用。

    关闭程序后它不会运行,除非您通过写入文件或数据库来保持状态。这很复杂,使用 cron 会更好。

    sched 适用于事件,但不适用于背景。因此,它并不完全是一个守护进程,但您可以使用操作系统工具在后台运行程序对其进行去守护进程。

    【讨论】:

    • 谢谢森西。但是如果您可以在 turbogear 中看到 docs.turbogears.org/TGScheduler,他们已经使用 sched 进行调度。如果我将预定的功能保留在服务中,即使在重新启动后它还能工作吗?
    • Kronos 和 TGScheduler 是对调度任务基本前提的更高层次的抽象。似乎定义了一些基于时间的具体任务(如每月的第一天),因此即使重新启动应用程序,这些任务也将起作用,但基于间隔的任务将不起作用(因为未维护会话)。他们将运行分叉到另一个任务,以便它在后台作为进程运行。正如我所说,他们似乎使用了更多(操作系统相关、线程和使用日期时间的具体事件)而不仅仅是 sched。
    • 感谢 senthil 的回复。我已经详细了解了 kronos。我的问题是:即使你为一个进程分叉或创建一个新线程并使用日期时间计算,如何在系统重启后继续相同的调度?
    • 它可能不是系统重启后运行的同一个调度程序,而是一个更新了计算的不同调度程序。例如,您将某些东西定义为在每个月的 6 日运行。因为这个事件是固定的,所以调度器可以在重启后工作。
    • 感谢senthil,但如何在系统重启时再次触发该事件?一旦系统重新启动,要么我们必须再次运行该事件,然后该调度程序将重新开始。系统重启时如何触发事件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2018-06-16
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多