행위

파이썬 스케줄러 모듈

DB CAFE

thumb_up 추천메뉴 바로가기


1 파이썬 스케줄러 모듈 - APSchedule[편집]

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

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

1.1 설치[편집]

  1. pip install apscheduler

1.2 사용예시[편집]

  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