Q:

how to fix threading in python

I wrote the following python code
import thread
from flask import Flask

app = Flask(__name__)
COOKIE =""
@app.route('/')
def index():
    return COOKIE

def request_cookie():
     global COOKIE
     while 1:
         %SOME CODE WHICH GET COOKIE FROM WEB SITE%
         sleep 5

if __name__ == '__main__':
    app.run(debug=True)
    t1 = thread.start_new_thread(get_cookie(), ())
    t1.start()

When I run this code. REST server starts but the thread doesn't start.
How can I fix it so that REST server starts and parallely runs the new thread to fetch cookie from a remote site.

A:

You are doing app.run(debug=True) which starts the web server and waits for it to complete. Since it doesn't complete till you terminate the server, the next line is not executed.
So for your thread to start, first start the thread and then start the server.
just change :
if __name__ == '__main__':
    t1 = thread.start_new_thread(get_cookie(), ())
    t1.start()
    app.run(debug=True)

