Run code after flask application has started

后端 未结 6 932
渐次进展
渐次进展 2020-12-09 14:54

My goal is to get arbitrary code to run after my Flask application is started. Here is what I\'ve got:

def run():
    from webapp import app
    app.run(debu         


        
6条回答
  •  甜味超标
    2020-12-09 15:06

    If you want to run a bunch of command (as a regular Py app) after flask run, use a multi processing library (for example multiprocessing), In this solution you can have an API/Web application beside of a system program.

    import flask
    from flask import request, jsonify,make_response
    import time
    import multiprocessing
    
    app = flask.Flask('__name__')
    
    def API(Conf):
       print('In API selction')
       app.run(host='0.0.0.0', port=1337,)
    if __name__ == "__main__":
       config = {"Something":"SomethingElese"}
       p = multiprocessing.Process(target=API, args=(Conf,))
       p.start()
       #time.sleep(3)
       print('After Flask run')
    

    Note: above code just a sample/idea. Maybe have some error. (But I used this solution in production area and it's fine.)

    P.S. : (in my perspective) the problem of this solution: more difficulty in debugging section.

提交回复
热议问题