메뉴 여닫기
개인 메뉴 토글
로그인하지 않음
만약 지금 편집한다면 당신의 IP 주소가 공개될 수 있습니다.

파이썬 스케줄러 모듈

DB CAFE

파이썬 스케줄러 모듈 - APSchedule

https://apscheduler.readthedocs.io/en/latest/

Advanced Python Scheduler 파이썬의 스케줄러 모듈입니다. cron 또는 특정 주기로 해당 기능을 실행 하고자 할 경우 사용

설치

  1. pip install apscheduler

사용예시

  1. scheduler.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-

from datetime import datetime
import time
import os

from apscheduler.schedulers.background import BackgroundScheduler


def tick():
    print('Tick! The time is: %s' % datetime.now())


if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    scheduler.add_job(tick, 'interval', seconds=3)
    scheduler.start()
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    try:
        # This is here to simulate application activity (which keeps the main thread alive).
        while True:
            time.sleep(2)
    except (KeyboardInterrupt, SystemExit):
        # Not strictly necessary if daemonic mode is enabled but should be done if possible
        scheduler.shutdown()
  1. 실행
# chmod 700 scheduler.py
# ./scheduler.py