1

How can I run my flask app in a separate thread? I am able to run my flask app in main, but

I need to launch the flask app from a thread, that thread should be running while in main thread I initiate an event for the thread engine to start.

so this works fine

@app.route("/voice", methods=['GET', 'POST']) def voice(): ......... return str(resp) @app.route("/mainFlow", methods=['GET', 'POST']) def mainFlow(): """Respond """ ....... return str(resp) if __name__ == "__main__": app.run(debug=True) 

but I need something like this

def myIVR(): print("-----------------Thrd1-myIVR --------------------") app = Flask(__name__) app.run(debug=True) @app.route("/voice", methods=['GET', 'POST']) def voice(): ........ return str(resp) @app.route("/mainFlow", methods=['GET', 'POST']) def mainFlow(): """Respond to """ resp = VoiceResponse() def myTest(): print("E2Etest") thrd1 = threading.Thread(target=myIVR, args=[]) thrd1.start() print("trigger event") #xyz() ################################################################ def main(): myTest() if __name__ == '__main__': main() 

I get this error

 File "C:\......\AppData\Local\Programs\Python\Python37-32\lib\signal.py", line 47, in signal handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler)) ValueError: signal only works in main thread 
5
  • did you check this linki stackoverflow.com/questions/53522052/… Commented Jul 2, 2019 at 15:57
  • Thank you, it looks like it has been asked about debug, I am wondering about threading. I also do know that I can run Flask in main thread, and I am NOT Using debugging. I need to run in threading mode. Commented Jul 2, 2019 at 16:33
  • Isvthis link answer your question or not ? Commented Jul 2, 2019 at 18:48
  • 1
    I have a working example you can crib from. github.com/davewsmith/lapse/blob/master/lapse.py#L31 is where I launch the thread to run Flask. Note in particular the use_reloader=False part, which avoids much sadness. Commented Jul 2, 2019 at 22:16
  • it works Dave thank you so much!!!! Commented Jul 2, 2019 at 22:49

1 Answer 1

1

try this :

from flask import Flask import threading app = Flask(__name__) def myIVR(): print("-----------------Thrd1-myIVR --------------------") app = Flask(__name__) threading.Thread(target=app.run).start() @app.route("/voice", methods=['GET', 'POST']) def voice(): return str(resp) @app.route("/mainFlow", methods=['GET', 'POST']) def mainFlow(): """Respond to """ resp = VoiceResponse() def myTest(): print("E2Etest") thrd1 = threading.Thread(target=myIVR, args=[]) thrd1.start() print("trigger event") #xyz() ################################################################ def main(): myTest() if __name__ == '__main__': main() 

or use the flask with debuging mode False it will fix your problem :

from flask import Flask import threading app = Flask(__name__) def myIVR(): print("-----------------Thrd1-myIVR --------------------") app = Flask(__name__) app.run(debug=False) @app.route("/voice", methods=['GET', 'POST']) def voice(): return str(resp) @app.route("/mainFlow", methods=['GET', 'POST']) def mainFlow(): """Respond to """ resp = VoiceResponse() def myTest(): print("E2Etest") thrd1 = threading.Thread(target=myIVR, args=[]) thrd1.start() print("trigger event") #xyz() ################################################################ def main(): myTest() if __name__ == '__main__': main() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.