我读过关于python sched (任务调度程序)的文章,它像cron一样工作。
但我有个问题:
调度程序在2小时后自动启动并运行该函数吗?还是我要在关闭系统后再启动?
sched像守护进程一样工作吗?
发布于 2010-12-30 03:22:07
所有三个问题的答案都是No。
sched和cron不同。它使用一个通用计时器或计数器函数和一个延迟函数,并允许您在特定时间(由通用计时器函数定义的事件)调度函数调用。
它不会在您关闭程序之后运行,除非您通过写入文件或数据库来维护状态。这很复杂,使用cron会更好。
赛义德工作的事件,但不是背景。因此,它并不完全是一个deamon,但是您可以使用OS工具在后台运行程序。
发布于 2010-12-30 03:53:33
如果是这样的话:
即使在系统重新启动之后,这也能工作吗?
答案是:否,那么涡轮齿轮调度程序如何能够在cron中使用cronos运行呢?涡轮增压器的预定事件也将在系统重新启动后消失。
如果我错了,请纠正我。
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()https://stackoverflow.com/questions/4559758
复制相似问题