我读过有关python sched(任务计划)的信息,它就像一个cron 但我有一个问题:
让我说如果我每隔2小时就调度一次ryn函数,同时我的系统会关闭,然后我再次重启系统...调度程序是否会自动启动并运行后的函数2小时?或者我必须在关闭系统后再次启动它?
做了像deamon一样的工作吗?
答案 0 :(得分:1)
对所有三个问题的回答是否。
sched与cron不同。它采用通用计时器或计数器功能和延迟功能,并允许您在特定时间(由通用计时器功能定义的事件)之后安排函数调用。关闭程序后它不会运行,除非您通过写入文件或数据库来维护状态。这很复杂,使用cron会更好。
sched适用于事件,但不适用于背景。所以,它并不完全是一个守护者,但你可以使用操作系统设备在后台运行该程序。\ / p>
答案 1 :(得分:0)
如果是这样的话:
即使在系统重启后这也会工作吗?
答案是:NO那么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()