简体   繁体   中英

Run Python script through the day only at times stored within a database

Run Python script through the day only at times stored within a database.

I have a database that indicates start times:

ID  Starttime
1   12:00
2   13:00
3   15:40 
4   18:00 
5   19:45

I would like to execute that script ONLY at those times. The python file is in my windows server. There are two options I can think of.

Implement an if statement within my code that says if(time=now) then do the rest of the script. But this would require me still running the script every 5min The other option I can think of is somehow setting the task scheduler to pull the times from the database in the morning and setting those times to run - to which I have no idea.

I think you can read https://pypi.python.org/pypi/schedule .

Here is an example:

import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)

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

schedule.every().day.at("10:30").do(job) is your want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM