Demonstrating APScheduler on a Flask App.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
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')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi,
Thanks a lot for the demonstration.. but being a noob in Python I do not understand why a is not imcrementing in this small variation of your code...
#!/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. """
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)
@app.route("/")
def home():
""" Function for test purposes. """
return "Welcome Home :) ! " + str(a) + " " + b
if name == "main":
app.run(debug = True, port = 5000, host= '192.168.1.24')