pythonのapschedulerでkeepaliveの練習する


## apschedulerについて

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

> [Advanced Python Scheduler](https://apscheduler.readthedocs.io/en/3.x/)
> 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`を使う場合

```python: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](https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html)
> `The scheduled_job() decorator works nicely too:`

```python: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()
```

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

### `CronTrigger`を使う場合

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

> [クーロン(cron)をさわってみるお](https://qiita.com/katsukii/items/d5f90a6e4592d1414f99)
[cronでn秒おきに実行するジョブを中途半端な一定期間だけ走らせたい](https://t-min.hatenablog.com/entry/2018/12/02/131534)

```python: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間隔で実行する場合は、`cron`や`scheduled_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の使い方（初心者向け）](https://programgenjin.hatenablog.com/entry/2019/04/01/093005)
[Advanced Python Scheduler](https://apscheduler.readthedocs.io/en/3.x/)
[https://github.com/agronholm/apscheduler](https://github.com/agronholm/apscheduler)
[https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html](https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html)
[https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html](https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html)
[Python で cron のように定期的にメソッドを実行する](https://blog.cles.jp/item/11238)
[クーロン(cron)をさわってみるお](https://qiita.com/katsukii/items/d5f90a6e4592d1414f99)
[cronでn秒おきに実行するジョブを中途半端な一定期間だけ走らせたい](https://t-min.hatenablog.com/entry/2018/12/02/131534)