question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

schedule with flask microframework

See original GitHub issue

Hi! 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:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
laubedcommented, Sep 24, 2016

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.

1reaction
dylwhichcommented, Aug 13, 2015

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:

def some_job():
    print("This job was scheduled from flask")

@app.route('/schedule/every/<int:count>/<period>')
def add_to_schedule(count=1, period=None):
    job = schedule.every(count)
    if count == 1:
        if period == 'day':
            job = job.day
        elif period == 'hour':
            job = job.hour
        elif period == 'minute':
            job = job.minute
        elif period == 'second':
            job = job.second
        else:
            return "Invalid period " + period
    else:
        if period == 'day':
            job = job.days
        elif period == 'hour':
            job = job.hours
        elif period == 'minute':
            job = job.minutes
        elif period == 'second':
            job = job.seconds
        else:
            return "Invalid period " + period
    job.do(some_job)
    return "OK"

And you could add more parameters and whatnot as necessary. Hope this helps!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found