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

ivanleoncz commented Aug 30, 2019

Hi @clemefr

Sorry for such late answer.

Here I have your same code, with a small fix and with comments about your issue, so you can make comparisons with your original implementation. I would like to go further on improving this, but I rather let you see this situation, and if want a better feedback regarding your example, I could describe here as well:

#!/usr/bin/python3
""" Demonstrating APScheduler feature for small Flask App with args """

from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask

a = 1
b = "22"

def sensor(a, b):
    """ Function for test purposes. """
    # Notice: this increment is happening under the scope of
    # sensor() function, and not outside of it.
    # That means, you are incrementing the 'a' variable which
    # is inside the sensor() function, and not the 'a' variable
    # which is OUTSIDE of the function.
    a = a + 1
    b = "33"
    print("Scheduler is alive!", a, b)

sched = BackgroundScheduler(daemon=True)
sched.add_job(sensor,'interval', seconds=5, args=[a,b])
sched.start()

b = "44"
print ("Final: ",a ,b)

app = Flask(__name__) # Here is '__name__', and not 'name'.

@app.route("/")
def home():
    """ Function for test purposes. """
    return "Welcome Home :) ! " + str(a) + " " + b

if __name__ == "main": # Here is '__name__', and not 'name'.
    app.run(debug=True, port=5000, host='0.0.0.0')

@silvaleal
Copy link

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

@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