코딩공부/Python Django

python schedule 라이브러리 사용하기

integerJI 2020. 9. 24. 23:25

Schedule란?

 

Schedule란 특정한 작업, 함수 수행을 주기적으로 실행하기 위해 사용하는 함수입니다.

 

예를들어 월,화,수,목,금 오전 7시에 알람을 울리게 설정한다면

 

평일 오전 7시에는 알람이 울리겠죠?

 

이것이 스케줄려입니다.

 

 

해당 글은 lemontia.tistory.com/508 의 lemontia님 글을 참고하여 작성하였습니다. 

 

python에서의 Schedule 사용

pip install schedule


pip를 이용해 schedule을 설치하여 줍니다.

 

schedule를 작성할 파일을 만들어줍니다.

 

만들며 주의사항 ==> integer-ji.tistory.com/318

 

schedule_test.py 파일 생성

from schedule import * 
import schedule
import time
 
def job():
    print("Schedule Test")

schedule.every(1).second.do(job)

count = 0

while True:
    schedule.run_pending()
    count += 1
    time.sleep(1)
    print(count)

 

해석은 job이라는 함수를 schedule를 통해 매 순간 1초마다 job을 실행시켜라

 

while문을 통해 무한 반복 시켜줍니다.

 

결과는

 

매 초마다 job 함수와 count에 + 1을 해준 값을 출력해 주고 있습니다.

 

every와 second와 같은 설정은

 

새로 설치한 lib인 schedule안에 보면 확인하실수 있습니다.

 

경로 : "C:\Users\사용자이름\AppData\Local\Programs\Python\Python38-32\lib\site-packages\schedule\__init__.py"