Is there a reason you cannot use either crontab or Windows Task Scheduler to schedule your jobs?
Answer One:
The schedule
module documentation does not indicate a simple way to schedule a python script to run every workday (monday to friday) at 8 PM CET.
Plus the use of timezones is not supported in the schedule
module.
Reference: 4.1.3 Does schedule support timezones?
Here is way to schedule a job to run every workday at 20:00 (8pm) using the schedule
module.
import schedule
import time
def schedule_actions():
# Every Monday task() is called at 20:00
schedule.every().monday.at('20:00').do(task)
# Every Tuesday task() is called at 20:00
schedule.every().tuesday.at('20:00').do(task)
# Every Wednesday task() is called at 20:00
schedule.every().wednesday.at('20:00').do(task)
# Every Thursday task() is called at 20:00
schedule.every().thursday.at('20:00').do(task)
# Every Friday task() is called at 20:00
schedule.every().friday.at('20:00').do(task)
# Checks whether a scheduled task is pending to run or not
while True:
schedule.run_pending()
time.sleep(1)
def task():
print("Job Running")
schedule_actions()
Answer Two:
I spent some additional time looking at using a scheduler inside a python script. During my research, I discovered the Python library -- Advanced Python Scheduler (APScheduler
).
This library seems to be very flexible based on the module's documentation.
Here is an example that I put together for you, which worked in my testing.
from apscheduler.schedulers.background import BlockingScheduler
# BlockingScheduler: use when the scheduler is the only thing running in your process
scheduler = BlockingScheduler()
# Other scheduler types are listed here:
# https://apscheduler.readthedocs.io/en/latest/userguide.html
# Define the function that is to be executed
# at a scheduled time
def job_function ():
print ('text')
# Schedules the job_function to be executed Monday through Friday at 20:00 (8pm)
scheduler.add_job(job_function, 'cron', day_of_week='mon-fri', hour=20, minute=00)
# If you desire an end_date use this
# scheduler.add_job(job_function, 'cron', day_of_week='mon-fri', hour=20, minute=00, end_date='2019-12-31')
# Start the scheduler
scheduler.start()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…