Start a flask application in separate thread

前端 未结 4 483
粉色の甜心
粉色の甜心 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:35

    Updated answer for Python 3 that's a bit simpler:

    from flask import Flask                                                         
    import threading
    
    data = 'foo'
    app = Flask(__name__)
    
    @app.route("/")
    def main():
        return data
    
    if __name__ == "__main__":
        threading.Thread(target=app.run).start()
    

提交回复
热议问题