LoginSignup
7
5

More than 3 years have passed since last update.

Pythonのscheduleライブラリでジョブ実行時に引数を渡す

Posted at

小ネタです。

Pythonのscheduleライブラリがスクリプトの定期実行になかなか便利なのですが、実行するジョブの中に引数を入れたい場合、通常の感覚とちょっと違うやり方だったので書いておきます。基本的な使い方は
https://qiita.com/dgkmtu/items/3bd3794b44a0aa03bfe3
を参考にしました。

結論として、ジョブに引数を入れて実行する場合には、以下のようにして動きます。

import schedule
import time

def job(x, y): # x, yを引数に持つジョブ
    print("job execute w/ argument: x = {}, y = {}".format(x,y))

# jobに引数を入れるときはdo(job, 引数)とする    
schedule.every().second.do(job, x=1, y=2)

while True:
    schedule.run_pending()
    time.sleep(1)

# 実行結果
# job execute w/ argument: x = 1, y = 2
# job execute w/ argument: x = 1, y = 2
# ...

感覚だと

schedule.every().second.do(job(x=1,y=2))

のようにしたくなるのですが、公式Referenceによると

schedule.every().second.do(job, x=1, y=2)

と、doの引数として使用するようです。

7
5
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
7
5