Search code examples
pythonflaskpython-multithreading

Start and Stop thread inside Flask


I am wondering is there something special I need to do for ending a thread job in Flask? Here is the sample code:

from flask import (
    Flask, jsonify, render_template, request
)
import threading
import time
import random

app = Flask(__name__)

def task(thread_return):
    random_seconds=random.random()
    print(f'Start task {threading.current_thread().name} ')
    time.sleep(random_seconds*15)
    print(f'completed {threading.current_thread().name} ')
    thread_return['seconds']= random_seconds*5

thread_return = {'seconds': 0}

        
@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("index-ajax.html")

    val = request.json.get("c_check")
    t =threading.Thread(target=task, args=(thread_return,), daemon=True)
    if val==1:
        print("val1 is ", val)
        t.start()
    elif val==0:
        try:
            print('trying to kill thread')
            exit_event= threading.Event()
            exit_event.set()
        except Exception as e:
            print(e)
        print("val0 is ", val)
    
    
    
    return jsonify({"data": {"val": val}})
    
if __name__ == "__main__":
    app.run(host="127.0.0.1", port=5000, debug=True)

I've tried incorporating several examples I saw online that wraps the task function inside a class, but it didn't work, namely: Exit Thread Gracefully

I like this other example, but I also couldn't get it to work with Flask: How Do You Kill a Python Thread

Thank you for your help.


Solution

  • I was trying multiple rewrites and noticed that the following works. Apparently, I needed to

    1. declare the threading.event() outside of the route function
    2. set the exit_event.set() inside the task function to end the task or prevent the task from running
    3. and modify the task function to better mimic a long or indefinite running task

    Perhaps someone else have a more elegant pythonic solution.


    from flask import (
        Flask, jsonify, render_template, request
    )
    import threading
    import time
    import random
    
    app = Flask(__name__)
    def task(thread_return):
        i=0
        while True:
            if exit_event.is_set(): 
                print(f'end {threading.current_thread().name} ')
                return
            i+=1
            random_seconds=random.random()
            print(f'Running task {threading.current_thread().name} {i} sec elapsed')
            time.sleep(1)
    
    exit_event= threading.Event()
            
    @app.route("/", methods=["GET", "POST"])
    def index():
        if request.method == "GET":
            return render_template("index-ajax.html")
    
        val = request.json.get("c_check")
        exit_event.clear()
        t =threading.Thread(target=task, args=(thread_return,), daemon=True)
        if val==1:
            print("val1 is ", val)
            t.start()
        elif val==0:
            try:
                print('trying to end thread')
                exit_event.set()
            except Exception as e:
                print(e)
            print("val0 is ", val)
        
        return jsonify({"data": {"val": val}})
        
    if __name__ == "__main__":
        app.run(host="127.0.0.1", port=5000, debug=True)