1

I'm writing a web app with Flask and when I try to create a countdown timer that can be started by high level user (allowing for a response by low level users in that time frame) and use time.sleep(1) in the countdown function, it freezes the web app for all users until the countdown is done.

How do I countdown in Flask without having the app freeze during the countdown? Is this a threading issue or do I need a different strategy?

@main.route('/class/<class_name>', methods = ['GET', 'POST'])
@login_required
def view_class(class_name):
    if class_name is None:
        abort(404)
    form = AttendanceForm()
    check_in_permitted = False
    c = Class.query.filter_by(id = class_name).first()
    if current_user.is_instructor():
        if form.validate_on_submit():
            t = int(form.seconds.data)
            check_in_permitted = True
            while t >= 0:
                print(t)
                time.sleep(1)
                t -= 1
            print("TIME")
            check_in_permitted = False

    elif current_user.is_user():
        if check_in_permitted == True:
            form = CheckInForm()

    return render_template('view_class.html', c = c, form = form)
user3246092
  • 880
  • 2
  • 13
  • 28
  • What is the purpose of the countdown? If its just a visual image showing numbers counting down, you don't need any server side code, just Javascript. – JacobIRR Jan 04 '18 at 17:44
  • I'm not very good with Javascript so I'm trying to stay server side as much as possible. I know how to do a timer like this in JS, but am unsure about connecting it to a Flask app -- since User B's actions need to be within the window set by User A starting the timer, I (assume) I have to go server side at some point – user3246092 Jan 04 '18 at 17:52

0 Answers0