Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

scheduling - How schedule a job run at 8 PM CET using schedule package in python

I want to schedule a python script to run at every workday (monday to friday) at 8 PM CET. How can this be done.

   import schedule
    import time

    def task():
        print("Job Running")

    schedule.every(10).minutes.do(task)

How can this be done.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...