简体   繁体   中英

How to start thread from another definition and stop thread from another definition in python?

The current behaviour is that whenever Question endpoint will be from webpage then t1 thread and t2 thread will run but I want the behaviour to be whenever done endpoint is called, both thread should stop both the definition is different. how can I do this?

@app.route('/Question')
def Question():
    t1 = threading.Thread(target=demoTask,args=())
    t2 = threading.Thread(target=demoTask1,args=())
    t1.start()
    t2.start()
    return render_template('questions.html')

@app.route('/done')
def done():
     return render_template('done.html')

I found the answer. we can create thread outside the definition in python. and we can start from any definition and we can stop from any definition in python. this is working for me.

t1 = threading.Thread(target=demoTask,args=())
t2 = threading.Thread(target=demoTask1,args=())
t3 = threading.Thread(target=demoTask2,args=())

@app.route('/instructions')
def instructions():
    return render_template('instructions.html')

@app.route('/thankyou')
def thankyou():
    return render_template('thankyou.html')

@app.route('/Question')
def Question():
    t1.start()
    t2.start()
    return render_template('questions.html')

@app.route('/done')
def done():

    t3.start()
    return render_template('done.html')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM