[PYTHON] Launch other processing after launching flask using multiprocessing

Trigger

I wanted to do other processing while starting flask, so I wrote that I could do something about it. It worked for the time being, so I will publish it. We have not verified it for a long time.

Operating environment

Python 3.9.1 flask 1.1.2

flask_hogehoge.py


from flask import Flask
from multiprocessing import Process
import datetime
import time

app = Flask(__name__)

@app.route('/')
def index():
        return "Hello World!"

def hogehoge():
    i = 0
    while True:
        print("{}, {}".format(i, datetime.datetime.now()))
        i += 1
        time.sleep(10)

def run(**kwargs):
    app.run(**kwargs)

if __name__ == '__main__':
    server = Process(target = run, kwargs = {'host': '0.0.0.0', 'port': 5001, 'threaded': True})
    server.start()

    hogehoge()

Commentary

Recommended Posts

Launch other processing after launching flask using multiprocessing