关于Python的调度问题

2024-09-26 22:13:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我读过python sched(task schedular)的文章,它就像一个cron。
但我有个问题:

  • 假设我每两个小时调度一个函数到ryn,同时我的系统关闭,然后我再次重新启动系统…调度程序是否自动启动并将在2小时后运行该函数?或者我必须在关闭系统后重新启动?

  • sched像个教士一样工作吗?


Tags: 函数程序task系统文章调度cron教士
2条回答

这三个问题的答案都是。在

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

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

sched处理事件,但不处理后台。所以,它并不完全是一个守护者,但是你可以用操作系统的工具在后台运行程序。在

如果是这样:
即使在系统重新启动后,这也能正常工作吗?
答案是:不,那么turbogear调度器如何在cron中使用cronos运行?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()

相关问题 更多 >

    热门问题