이전에 올렸던 시칸 체크 클래스와 apscheduler 를 이용하여 주기적 실행 코드를 작성해 보았다.
2019/11/07 - [Python/Base] - 시작 시간, 종료 시간 리스트가 현재 시간에 포함되어있는지 체크
해당 기능을 이용하려면 apscheduler 를 설치해야 된다.
pip install apscheduler
아래 코드는 어쩐지 오늘은 님의 코드를 참고하여 시간 체크 클래스를 응용하여 만들었다.
원본글
https://zzsza.github.io/development/2018/07/07/python-scheduler/
Execute!! 부분에 실행 로직을 추가 하면 된다.
from apscheduler.jobstores.base import JobLookupError
from apscheduler.schedulers.background import BackgroundScheduler
import time
from TimeIncludeChk import TimeIncludeChk
class Scheduler:
def __init__(self, time_chk: TimeIncludeChk):
self.schedule = BackgroundScheduler()
self.schedule.start()
self.job_id = '1MinuteWork'
self.timeChk = time_chk
def __del__(self):
self.shutdown()
def shutdown(self):
self.schedule.shutdown()
def kill_scheduler(self):
try:
self.schedule.remove_job(self.job_id)
except JobLookupError as err:
print("fail to stop Scheduler: {err}".format(err=err))
return
def execute_work(self):
print("Scheduler process_id[Job] : %d" % (time.localtime().tm_sec,))
print("is_include_time_now : ", self.timeChk.is_include_time_now())
if self.timeChk.is_include_time_now():
print("Execute!!!")
def scheduler(self):
print("Scheduler Start")
self.schedule.add_job(self.execute_work, 'interval', minutes=1, id=self.job_id)
if __name__ == '__main__':
datas = [
{'no': 276, 'fountainNo': 'DE0000000007', 'dayWeek': 'MON', 'dayWeekIdx': 0, 'startHour': 9, 'startMin': 0,
'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000,
'offLight': 'Y'},
{'no': 283, 'fountainNo': 'DE0000000007', 'dayWeek': 'MON', 'dayWeekIdx': 0, 'startHour': 13, 'startMin': 0,
'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000,
'offLight': 'N'},
{'no': 277, 'fountainNo': 'DE0000000007', 'dayWeek': 'TUE', 'dayWeekIdx': 1, 'startHour': 9, 'startMin': 0,
'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000,
'offLight': 'Y'},
{'no': 284, 'fountainNo': 'DE0000000007', 'dayWeek': 'TUE', 'dayWeekIdx': 1, 'startHour': 13, 'startMin': 0,
'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000,
'offLight': 'N'},
{'no': 278, 'fountainNo': 'DE0000000007', 'dayWeek': 'WED', 'dayWeekIdx': 2, 'startHour': 9, 'startMin': 0,
'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000,
'offLight': 'Y'},
{'no': 285, 'fountainNo': 'DE0000000007', 'dayWeek': 'WED', 'dayWeekIdx': 2, 'startHour': 13, 'startMin': 0,
'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000,
'offLight': 'N'},
{'no': 286, 'fountainNo': 'DE0000000007', 'dayWeek': 'THU', 'dayWeekIdx': 3, 'startHour': 13, 'startMin': 0,
'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000,
'offLight': 'N'},
{'no': 279, 'fountainNo': 'DE0000000007', 'dayWeek': 'THU', 'dayWeekIdx': 3, 'startHour': 9, 'startMin': 0,
'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000,
'offLight': 'Y'},
{'no': 280, 'fountainNo': 'DE0000000007', 'dayWeek': 'FRI', 'dayWeekIdx': 4, 'startHour': 9, 'startMin': 0,
'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000,
'offLight': 'Y'},
{'no': 287, 'fountainNo': 'DE0000000007', 'dayWeek': 'FRI', 'dayWeekIdx': 4, 'startHour': 13, 'startMin': 0,
'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000,
'offLight': 'N'},
{'no': 281, 'fountainNo': 'DE0000000007', 'dayWeek': 'SAT', 'dayWeekIdx': 5, 'startHour': 9, 'startMin': 0,
'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000,
'offLight': 'Y'},
{'no': 288, 'fountainNo': 'DE0000000007', 'dayWeek': 'SAT', 'dayWeekIdx': 5, 'startHour': 13, 'startMin': 0,
'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000,
'offLight': 'N'},
{'no': 282, 'fountainNo': 'DE0000000007', 'dayWeek': 'SUN', 'dayWeekIdx': 6, 'startHour': 9, 'startMin': 0,
'startTime': '09:00', 'endHour': 9, 'endMin': 50, 'endTime': '09:50', 'inputNo': 18, 'regDate': 1572910002000,
'offLight': 'Y'},
{'no': 289, 'fountainNo': 'DE0000000007', 'dayWeek': 'SUN', 'dayWeekIdx': 6, 'startHour': 13, 'startMin': 0,
'startTime': '13:00', 'endHour': 13, 'endMin': 50, 'endTime': '13:50', 'inputNo': 19, 'regDate': 1572910025000,
'offLight': 'N'}
]
chk = TimeIncludeChk()
chk.set_time_list(datas)
scheduler = Scheduler(chk)
scheduler.scheduler()
count = 0
while True:
time.sleep(60)
count += 1
if count == 150:
scheduler.kill_scheduler()
print("Kill cron Scheduler")
'Python > Base' 카테고리의 다른 글
사업자 번호 유효 여부 크롤링 (0) | 2023.06.05 |
---|---|
시작 시간, 종료 시간 리스트가 현재 시간에 포함되어있는지 체크 (0) | 2019.11.07 |