Python/schedule

From Fundamental Ramen
Jump to navigation Jump to search

Some tests

# Okay
schedule.every().wednesday.at('02:28').do(job)
# No effect
schedule.every().wednesday.thursday.at('02:28').do(job)
# 02:56:30 works only
schedule.every().wednesday.at('02:56:00').at('02:56:30').do(job)

standard

import datetime
import time
import schedule

def job():
  isotime = datetime.datetime.now().strftime('%H:%M:%S')
  print("{} | I'm working.".format(isotime))

schedule.every(3).seconds.do(job)
while True:
  schedule.run_pending()
  time.sleep(1)
18:37:57 | I'm working.
18:38:00 | I'm working.
18:38:03 | I'm working.
18:38:06 | I'm working.
18:38:09 | I'm working.

long time job

import datetime
import time
import schedule

def job():
  isotime = datetime.datetime.now().strftime('%H:%M:%S')
  print("{} | I'm working.".format(isotime))
  time.sleep(2)

schedule.every(3).seconds.do(job)
while True:
  schedule.run_pending()
  time.sleep(1)
18:40:16 | I'm working.
18:40:21 | I'm working.
18:40:26 | I'm working.
18:40:31 | I'm working.
18:40:36 | I'm working.

long time job with threading

import datetime
import schedule
import threading
import time

def parallel(func):
  def pre_hook(*argv):
    t = threading.Thread(target=func)
    t.start()
  return pre_hook

@parallel
def job():
  isotime = datetime.datetime.now().strftime('%H:%M:%S')
  print("{} | I'm working.".format(isotime))
  time.sleep(2)

schedule.every(3).seconds.do(job)
while True:
  schedule.run_pending()
  time.sleep(1)
11:09:35 | I'm working.
11:09:38 | I'm working.
11:09:41 | I'm working.
11:09:44 | I'm working.
11:09:47 | I'm working

long time job with asynchronous

import asyncio
import datetime
import schedule
import time

async def task():
  isotime = datetime.datetime.now().strftime('%H:%M:%S')
  print("{} | I'm working.".format(isotime))
  await asyncio.sleep(2)

def job():
  asyncio.run(task())

schedule.every(3).seconds.do(job)
while True:
  schedule.run_pending()
  time.sleep(1)
11:15:53 | I'm working.
11:15:58 | I'm working.
11:16:03 | I'm working.
11:16:08 | I'm working.
11:16:13 | I'm working.