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.
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()
def hogehoge ()
, write what you want to process after starting flask. server = Process(...
Where
target = app.run, kwargs = {...
I wrote, but I couldn't pass the argument to app.run well, so
Use def run (** kwargs)
to make it app.run
target = run, kwargs = {...
.Recommended Posts