schedule with flask microframework
See original GitHub issueHi! how can i use schedule as a cron job with flask microframework? This is the answer i found on stackoverflow
# -*- coding: utf-8 -*-
"""
This script is a simple example you can use to schedule task with flask
microframework and schedule (https://github.com/dbader/schedule).
I've found it on on stackoverflow!
"""
import time
import schedule
from flask import Flask, request
from threading import Thread
app = Flask(__name__)
start_time = time.time()
def run_every_10_seconds():
print("Running periodic task!")
print "Elapsed time: " + str(time.time() - start_time)
def run_schedule():
while 1:
schedule.run_pending()
time.sleep(1)
@app.route('/', methods=['GET'])
def index():
return '<html>test</html>'
if __name__ == '__main__':
schedule.every(10).seconds.do(run_every_10_seconds)
t = Thread(target=run_schedule)
t.start()
print "Start time: " + str(start_time)
app.run(debug=True, host='0.0.0.0', port=5000, use_reloader=False)
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Welcome to Flask — Flask Documentation (2.2.x)
Welcome to Flask's documentation. Get started with Installation and then get an overview with the Quickstart. There is also a more detailed Tutorial...
Read more >Run Your Flask Regularly Scheduled Jobs with Cron
A scheduled job is given in the crontab file as a line with six fields. The first five fields are used to set...
Read more >Flask [ Python Microframework ] Crash Course 2021 For ...
00:00 Presentation ; 02:44 Course Plan ; 03:25 Minimal Application [setup] ; 07:06 Routes ('/index') ; 08:22 Rendering HTML templates ...
Read more >What is Flask Python
Flask is a web framework, it's a Python module that lets you develop web applications easily. It's has a small and easy-to-extend core:...
Read more >Leveraging Flask Micro-Framework with Python
When you're finished, save, and exit the program. You can test your Flask app by typing: python projectname.py inside your virtual environment.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You are missing the worker thread part in main. It is neccessary because this thread will call your functions. Right now there is no such thread in your app.
What are you trying to do? Do you want to have a flask API that can schedule jobs on command? For that you could add something like this:
And you could add more parameters and whatnot as necessary. Hope this helps!