LoginSignup
5
3

More than 3 years have passed since last update.

pythonによるスケジュール(計画実行)プログラム

Posted at

ジョブスケジューリングするプログラムを書くのは、かなりめんどくさい。時には、追加したり削除したりすべてを取り消したり、再起動したり、様々な要求に対応しなければならない。

image.png
英文のマニュアルはurl

注意

  • ジョブの永続性については、考慮されていません。
  • 正確なタイミング(1秒未満の精度の実行)は、できません。
  • 同時実行(複数スレッド)
  • ローカリゼーション(タイムゾーン、平日または休日)は、考慮してありません。

インストール方法は、以下の通りです。

$ pip install schedule

準備

スケジュール関係をimportします。

import schedule,time,datetime

実行する関数を定義します。(複数回実行する場合)

def job():
    s=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    print("I'm working..."+s)

一回のみの実行する関数の定義(スリープモードのような一定時間後に停止するようなタイマー)

def job():
    s=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    print("I'm working..."+s)
    return schedule.CancelJob()

schedule.CancelJob()を呼ぶことにより次回以降のスケジュールをキャンセルします。

基本ルーチン1

10秒に一回job関数を実行します。

import schedule,time,datetime
def job():
    s=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    print("I'm working..."+s)
schedule.every(5).seconds.do(job)
while True:
    schedule.run_pending()
    time.sleep(1)

基本ルーチン2

時間formatの指定

import schedule,time,datetime

def job():
    s=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    print("I'm working..."+s)
schedule.every(1).minutes.at(':00').do(job)
while True:
    schedule.run_pending()
    time.sleep(1)

基本ルーチン3

毎日10:30にjobを実行します

import schedule,time,datetime

def job():
    s=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    print("I'm working..."+s)
#毎日10:30にjobを実行します
schedule.every().day.at("10:30").do(job)

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

基本ルーチン4

毎週月曜日にjobを実行します

import schedule,time,datetime

def job():
    s=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    print("I'm working..."+s)
#毎週月曜日にjobを実行します
schedule.every().monday.do(job)

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

基本ルーチン5

毎週水曜日の13時15分にjobを実行します

import schedule,time,datetime

def job():
    s=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    print("I'm working..."+s)
#毎週水曜日の13時15分にjobを実行します
schedule.every().wednesday.at("13:15").do(job)

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

基本ルーチン6

一度に複数のジョブをキャンセルする

コーヒーメーカなどの制御で外出時等にスケジールをキャンセル必要がある場合に使用します。下記の通りタグを設けそのグループをキャンセルします。

import schedule,time,datetime

def coffee_on():
    s=datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    print("I'm working..."+s)

#外出時にコーヒーメーカのスケジュールを一斉にキャンセルする
def Can_coffee():
    schedule.clear('coffee')

#コーヒーメーカーの電源管理
def CoffeeSchedule():
    schedule.every().day.at("06:30").do(coffee_on).tag("coffee")
    schedule.every().day.at("12:30").do(coffee_on).tag("coffee")
    schedule.every().day.at("15:30").do(coffee_on).tag("coffee")
    schedule.every().day.at("17:30").do(coffee_on).tag("coffee")
CoffeeSchedule()
while True:
    schedule.run_pending()
    time.sleep(1)

基本ルーチン7

リビングの照明を1時間後に消灯する

import schedule,time,datetime


def printTime():
    print(datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S'),end=" ")

def sleepTimer():
    printTime()
    print("light off")
    schedule.clear()

#リビングの電灯管理
def SleepLight(timer=60):
    printTime()
    print("light on")
    schedule.every(timer).minutes.do(sleepTimer)
SleepLight(60)
while True:
    schedule.run_pending()
time.sleep(1)
5
3
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
5
3