В приведенном ниже коде:
from flask import Flask
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
from time import sleep
app = Flask(__name__)
scheduler = BackgroundScheduler(timezone="UTC", daemon=True)
def my_job(var):
print(var)
scheduler.add_job(func=lambda: my_job("something"),
trigger="cron",
start_date="2019-11-05 18:31:00",
end_date="2019-11-05 18:31:05")
scheduler.start()
@app.route('/')
def index():
while True:
year = datetime.now().year
month = datetime.now().month
day = datetime.now().day
hour = datetime.now().hour
minute = datetime.now().minute
second = datetime.now().second
print("""Current date and hour: {year}-{month}-{day} {hour}:{minute}:{second}
""".format(year=year, month=month, day=day, hour=hour, minute=minute, second=second))
print("Scheduler state: {state}".format(state=scheduler.state))
sleep(0.99)
return 'ok'
if __name__ == "__main__":
app.run(debug=False)
Учитывая, что текущая дата - 2019-11-05, а текущий час - 18:30:57 ... вывод приведенного выше кода:
Current date and hour: 2019-11-5 18:30:57
Scheduler state: 1
Current date and hour: 2019-11-5 18:30:58
Scheduler state: 1
Current date and hour: 2019-11-5 18:30:59
Scheduler state: 1
something
Current date and hour: 2019-11-5 18:31:0
Scheduler state: 1
something
Current date and hour: 2019-11-5 18:31:1
Scheduler state: 1
something
Current date and hour: 2019-11-5 18:31:2
Scheduler state: 1
something
Current date and hour: 2019-11-5 18:31:3
Scheduler state: 1
something
Current date and hour: 2019-11-5 18:31:4
Scheduler state: 1
something
Current date and hour: 2019-11-5 18:31:5
Scheduler state: 1
Current date and hour: 2019-11-5 18:31:6
Scheduler state: 1
Current date and hour: 2019-11-5 18:31:7
То есть ... даже до, во время и после, он печатает 1. Я хочу знать, когда модуль работает только в запрограммированную дату, то есть, чтобы напечатать 0 до, 1 во время и 0 после.