Skip to content

Instantly share code, notes, and snippets.

@ivanleoncz
Last active April 1, 2024 04:06
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ivanleoncz/21293b00d0ea54db8ee3b57fb1170ddf to your computer and use it in GitHub Desktop.
Save ivanleoncz/21293b00d0ea54db8ee3b57fb1170ddf to your computer and use it in GitHub Desktop.
Demonstrating APScheduler on a Flask App.
#!/usr/bin/python3
""" Demonstrating APScheduler feature for small Flask App. """
from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask
def sensor():
""" Function for test purposes. """
print("Scheduler is alive!")
sched = BackgroundScheduler(daemon=True)
sched.add_job(sensor,'interval',seconds=60)
sched.start()
app = Flask(__name__)
@app.route("/home")
def home():
""" Function for test purposes. """
return "Welcome Home :) !"
if __name__ == "__main__":
app.run()
@ivanleoncz
Copy link
Author

Hello, I have to do a task every 15 seconds AFTER Flask is turned on, does this library work for me?

It seems so. I forked the code above on my machine and then added a statement for printing the timestamp of each execution of sensor():

 $ python3 apscheduler_demo.py 
 * Serving Flask app 'apscheduler_demo'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
Scheduler is alive! 2024-03-31 22:01:19.265945
Scheduler is alive! 2024-03-31 22:01:34.265660
Scheduler is alive! 2024-03-31 22:01:49.265825
Scheduler is alive! 2024-03-31 22:02:04.265684
Scheduler is alive! 2024-03-31 22:02:19.265957
Scheduler is alive! 2024-03-31 22:02:34.265683
Scheduler is alive! 2024-03-31 22:02:49.265763

Worth reading the API documentation tough, besides making your own tests in order to validate if the library suits your needs or not:

Regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment