Ghost
Ghost

Reputation: 579

python How can I run flask app in a thread?

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

Upvotes: 1

Views: 2696

Answers (1)

Ghassen
Ghassen

Reputation: 794

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()

Upvotes: 1

Related Questions