0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonのapschedulerでkeepaliveの練習する

Posted at

apschedulerについて

以下の通り、定期実行のためのpython library

Advanced Python Scheduler
Advanced Python Scheduler (APScheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or periodically.

インストール

https://pypi.org/project/APScheduler/

pip3 install apscheduler

使い方

cronを使う場合

test.py
# -*- coding: utf-8 -*-                                                                                                                                                                                     

from apscheduler.schedulers.blocking import BlockingScheduler

def keep_alive():
    print("keep_alive!!")

sc = BlockingScheduler()

# Schedules job_function to be run on every 10 seconds                                                                                                                                                      
sc.add_job(keep_alive, 'cron', month='*', hour='*', minute='*', second='0,10,20,30,40,50')

def main():
    sc.start()

main()
$ python3 test.py
keep_alive!!
keep_alive!!
...(以降10sec毎にkeepalive)

scheduled_job() decoratorを使う場合

https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html
The scheduled_job() decorator works nicely too:

test.py
# -*- coding: utf-8 -*-                                                                                                                                                                                     

from apscheduler.schedulers.blocking import BlockingScheduler

sc = BlockingScheduler()

def keep_alive():
    print("keep_alive!")

@sc.scheduled_job('interval', seconds=10) #"minutes=10                                                                                                                                                       
def do_work():
    keep_alive()

def main():
    sc.start()

main()
$ python3 test.py
keep_alive!
keep_alive!
keep_alive!
keep_alive!
...(以降10sec毎にkeepalive)

CronTriggerを使う場合

以下のcronのフォーマットを参考にする

クーロン(cron)をさわってみるお
cronでn秒おきに実行するジョブを中途半端な一定期間だけ走らせたい

test.py
# -*- coding: utf-8 -*-                                                                                                                                                                                     

from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger

def keep_alive():
    print("keep_alive!!")

sc = BlockingScheduler()

# Schedules job_function to be run on every 10 seconds                                                                                                                                                      
#sc.add_job(keep_alive, 'cron', month='*', hour='*', minute='*', second='0,10,20,30,40,50')                                                                                                                 
sc.add_job(keep_alive, CronTrigger.from_crontab('*/1 * * * *'))

def main():
    sc.start()

main()
$ python3 test.py
keep_alive!!
keep_alive!!
...(以降1min毎にkeepalive)

通常CronTrigger.from_crontabでは、1min間隔が最小間隔となる。
1sec間隔で実行する場合は、cronscheduled_job() decoratorを使う場合

PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.htmlのwarningが出る

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/apscheduler/util.py:95: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html

以下の通りtzlocalのバージョンが起因しているようなので、tzlocalのバージョンを更新すると解決した。

https://github.com/agronholm/apscheduler/discussions/570
This happens with tzlocal 4.0. You can either (safely) ignore those warnings or downgrade tzlocal to 2.x. The problem is "properly" fixed on master but likely won't be in the 3.x branch.

pip3 install tzlocal==2.1

ただし、記載の注意点の通り、他の依存関係に影響あるかもしれないので注意

But, please, pay attention it could break other project dependencies, so be careful.

#参考
APSchedulerの使い方(初心者向け)
Advanced Python Scheduler
https://github.com/agronholm/apscheduler
https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html
Python で cron のように定期的にメソッドを実行する
クーロン(cron)をさわってみるお
cronでn秒おきに実行するジョブを中途半端な一定期間だけ走らせたい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?