Start a flask application in separate thread

前端 未结 4 436
粉色の甜心
粉色の甜心 2020-11-27 17:51

I\'m currently developing a Python application on which I want to see real-time statistics. I wanted to use Flask in order to make it easy to use and to underst

4条回答
  •  有刺的猬
    2020-11-27 18:29

    If you are looking for accessing iPython terminal in Flask run your application in a separate thread. Try this example:

    from flask import Flask                                                         
    import thread
    
    data = 'foo'
    app = Flask(__name__)
    
    @app.route("/")
    def main():
        return data
    
    def flaskThread():
        app.run()
    
    if __name__ == "__main__":
        thread.start_new_thread(flaskThread, ())
    

    (Run this file in iPython)

提交回复
热议问题