편리한 Python 생활은 계속됩니다.
지정시간에 원하는 정보를 주기적으로 받아보고자 합니다.
그러기 위해서는 schedule 함수를 이용하여 정해진 시간 또는 주기로 정보를 받아보려고 합니다.
우선 schedule 함수를 install 합니다.
pip install schedule |
Schedule함수에 대해서 알아보겠습니다.
GitHub - dbader/schedule: Python job scheduling for humans.
Python job scheduling for humans. Contribute to dbader/schedule development by creating an account on GitHub.
github.com
API는 아래 페이지를 참조하세요.
schedule — schedule 1.1.0 documentation
schedule.readthedocs.io
간단한 sample 예제를 작성해 보았습니다.
1. schedule 함수와 time 함수를 import 합니다.
import schedule
import time
2. 실제 동작시킬 함수를 정의 합니다.
간단히 "Repeat the result"를 반복적으로 쓰도록 하겠습니다.
def repeat():
print("Repeat the result")
3. 위에서 define한 repeat 함수를 3초마다 반복할수 있도록 하기 위해 실행영역에 함주 정의를 해줍니다.
schedule.every(3).seconds.do(repeat) # 3초마다 job 실행
while True:
schedule.run_pending()
time.sleep(1)
위의 내용을 합치면 아래와 같이 됩니다.
import schedule
import time
def repeat():
print("Repeat the result")
schedule.every(3).seconds.do(repeat) # 3초마다 job 실행
while True:
schedule.run_pending()
time.sleep(1)
Repeat 함수 사용방법을 보면 아래와 같습니다.
schedule.every(10).seconds.do(함수) #함수를 10초마다 실행
schedule.every(10).minutes.do(함수) #함수를 10분마다 실행
schedule.every().hour.do(함수) #함수를 시간마다 실행
schedule.every().day.at("10:30").do(함수) #함수를 매일 10시 30분에 실행
schedule.every().monday.do(함수) #함수를 매주 월요일에 실행
schedule.every().wednesday.at("13:15").do(함수) #함수를 매주 수요일 13: 15분에 실행
이번에도 제가 에러가 나는 경우를 하나 경험했는데.....
다음 게시글로 설명드리겠습니다.... I'm an idiot, too!!! ㅠㅠ
https://spica910.tistory.com/221
[python] schedule error "module 'schedule' has no attribute 'every'
제가 코드를 짜면서 매번 에러들때문에 고생하는데 이번에도 하나 걸려 나왔습니다. 보자마자 대번 알아보시는 분들도있겠지만... 한참 구글링을 했습니다. 동일한 error를 호소하시는 분을 github
spica910.tistory.com
'Engineering > Python' 카테고리의 다른 글
[python] google sheet 정보를 정해진 시간에 telegram으로 보내기 (0) | 2022.12.25 |
---|---|
[python] schedule error "module 'schedule' has no attribute 'every' (0) | 2022.12.11 |
[python] 무료 서버 사용 (Feat. 구름 IDE 항상켜두기) (0) | 2022.07.17 |
[python] 텔레그램 bot 만들기 (2) | 2022.07.08 |
[python] openpyxl을 이용한 Excel 제어 (0) | 2022.07.02 |
댓글