1 ๋ถ„ ์†Œ์š”

Python ์Šค์ผ€์ค„๋ง

์Šค์ผ€์ค„๋ง์ด๋ž€, ๋‚ด๊ฐ€ ์‹ค์ œ ๋™์ž‘์‹œํ‚ค์ง€ ์•Š์•„๋„ ํŠน์ • ์‹œ๊ฐ„์ด ๋˜๋ฉด ์ฝ”๋“œ ์ƒ์—์„œ ์ž๋™์œผ๋กœ ๋™์ž‘ํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ์ฃผ์‹ ์ •๋ณด๋ฅผ ํฌ๋กค๋ง ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ ๋‹ค๊ณ  ํ•  ๋•Œ, ์ฃผ๊ฐ€ ๋ณ€๋™์„ ๊ณ ๋ คํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์‹œ๊ฐ„๋งˆ๋‹ค ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์Šค์ผ€์ค„๋Ÿฌ์—๋Š” BackgraoundScheduler์™€ BlockingScheduler๊ฐ€ ์žˆ๋‹ค.
ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋„ ๊ณ„์†ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด BackgraoundScheduler๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
์‹คํ–‰ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋‹จ์ผ ํ”„๋กœ์„ธ์Šค๋ผ๋ฉด BlockingScheduler๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

1. schedule

ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์Šค์ผ€์ค„๋Ÿฌ ๋ชจ๋“ˆ์ด ์žˆ๋‹ค.

pip install schedule

์œ„ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ schedule๋ชจ๋“ˆ์„ ์„ค์น˜ํ•œ๋‹ค.

[์˜ˆ์‹œ]

import schedule
import time
import datetime

#์Šค์ผ€์ฅด ๋ชจ๋“ˆ์ด ๋™์ž‘์‹œํ‚ฌ ์ฝ”๋“œ : ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅ
def test_fuction():
    now = datetime.datetime.now()
    print("test code- ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ")
    print(now)
    
#1์ดˆ๋งˆ๋‹ค test_fuction์„ ๋™์ž‘์‹œํ‚ค์ž
schedule.every(1).seconds.do(test_fuction)

#๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ ์Šค์ผ€์ฅด์„ ์œ ์ง€ํ•œ๋‹ค.
while True:
    schedule.run_pending()
    time.sleep(1)
  • ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด schedule.every(1).seconds.do(test_fuction)๋ฅผ ์ž‘์„ฑํ•˜๋ฉด 1์ดˆ๋งˆ๋‹ค test_fuction์„ ๋™์ž‘์‹œํ‚ค๊ฒŒ ๋œ๋‹ค.

[๊ฒฐ๊ณผ]

2021-05-16 22:08:08.464882
test code- ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ
2021-05-16 22:08:09.480980
test code- ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ
2021-05-16 22:08:10.492642
test code- ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ
2021-05-16 22:08:11.507662
test code- ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ
2021-05-16 22:08:12.513250

2. apscheduler

apscheduler๋Š” ํŒŒ์ด์ฌ ๊ณต์‹ ๋ฌธ์„œ์— ์ฐธ์กฐ ๋˜์–ด์žˆ๋‹ค.
๊ณต์‹๋ฌธ์„œ

์ˆ˜ํ–‰๋ฐฉ์‹
  1. Cron ๋ฐฉ์‹ - Cron ํ‘œํ˜„์‹์œผ๋กœ ์ˆ˜ํ–‰
  1. Date ๋ฐฉ์‹ - ํŠน์ • ๋‚ ์งœ์— ์ˆ˜ํ–‰
  1. Interval ๋ฐฉ์‹ - ์ผ์ • ์ฃผ๊ธฐ๋กœ ์ˆ˜ํ–‰


์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์„œ ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•œ๋‹ค.

pip install apscheduler

[์˜ˆ์‹œ]

def job(data):
    print('data : ' + data)


def main():
    job('hello?')

if __name__ == "__main__":
    main()

์œ„ ์ฝ”๋“œ๋Š” โ€œhello?โ€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” job()๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ์ฝ”๋“œ์ด๋‹ค.
job()์„ 3์ดˆ์— ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰์‹œํ‚ค๊ณ  ์‹ถ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

from apscheduler.schedulers.background import BlockingScheduler

def job(data):
    print('data : ' + data)


def main():
    # sched = BlockingScheduler()
    sched = BlockingScheduler(timezone='Asia/Seoul')    # ์„œ์šธ ์‹œ๊ฐ„์œผ๋กœ ๋งž์ถค
    sched.add_job(job,'interval', seconds=3, id='test',args=['hello?'])
    sched.start()

if __name__ == "__main__":
    main()

์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด 3์ดˆ์— ํ•œ ๋ฒˆ์”ฉ ์ž๋™์œผ๋กœ ์‹คํ–‰ํ•˜๊ฒŒ๋œ๋‹ค.
์‹คํ–‰ํ•˜๊ณ ๋‚˜๋ฉด, ๋‚ด๊ฐ€ ๋ฉˆ์ถ”๊ธฐ ์ „๊นŒ์ง€๋Š” ๊ณ„์† ๋Œ์•„๊ฐ„๋‹ค.

์ฃผ์˜ํ•  ์ 
add_job์˜ ์ธ์ˆ˜, โ€˜intervalโ€™์ด ์ฃผ๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์คŒ.
args๋Š” ๋ฐ˜๋“œ์‹œ ๋ฆฌ์ŠคํŠธ์˜ ํ˜•ํƒœ๋ฅผ ๋„์–ด์•ผ ํ•จ.
id๋Š” ์™œ ์žˆ์„๊นŒ?ย ยป ์ด ์ „์— job()์ด ์‹คํ–‰๋˜๊ณ , 3์ดˆ๊ฐ€ ์ง€๋‚˜ ๋‹ค์‹œ job()์ด ์‹คํ–‰๋˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ๊ฐ™์€ id๊ฐ’์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰์ค‘์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธํ•˜๋Š” ์šฉ๋„์ธ ๋“ฏ ํ•˜๋‹ค.


์ฐธ๊ณ  ๋ฌธ์„œ ๋ฐ ๋ธ”๋กœ๊ทธ
https://lemontia.tistory.com/508
https://ffoorreeuunn.tistory.com/466
https://ybworld.tistory.com/74

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ