开发者

Python sched scheduler and reboots

I have read about python sched (task scheduler), it works like a cron.

but I have a question :

  • lets say if I schedule a function t开发者_如何学运维o run after every 2 hours and in the mean time my system gets shut down, then I again restart the system...

Does the scheduler automatically start and run the function after 2 hours? Or do I have to start that again after shutting down the system?

  • Does sched work like a daemon?


Answer to all three questions is No.

sched is different from cron. It takes a generic timer or counter function and a delay function and lets you to schedule a function call after a particular time (an event as defined by your generic timer function).

It won't run after you close your program, unless you maintain state by writing to a file or db. This is complicated and using cron would be better.

sched works on events, but not on background. so, it not is not exactly a deamon, but you can deamonize it running the program in the background using OS facilities.


and if this is so :
will this also work even after system restart?
answer is : NO then how can turbogear scheduler can run using cronos in cron? scheduled events in turbogear will also be vanished after SYSTEM restart.
PLEASE CORRECT ME IF I AM WRONG.

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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜