阻止Flask初始化被阻止 [英] Stopping Flask initialization from blocking
问题描述
我正在将Flask支持添加到基于插件的应用程序中.在启动时,该应用程序实例化许多插件类.我认为这就像在初始化类时启动Flask一样简单,但是相反,整个应用程序在遇到Flask启动方法时就会挂起.
I'm adding Flask support to a plugin-based application. On startup, the app instantiates a number of plugin classes. I thought this would be as simple as having Flask kick off when the class is initialized, but instead, the whole app hangs when it hits the Flask startup method.
请考虑以下示例:
#!/usr/bin/env python
from flask import Flask
class TestClass:
def __init__(self):
print('Initializing an instance of TestClass')
self.app = Flask(__name__)
self.app.run()
print("Won't get here until Flask terminates!")
foo = TestClass()
在Flask终止之前,不会评估第二条打印线.
The second print line won't be evaluated until Flask is terminated.
是否有一种明智的方式将 app.run
强制到后台,以便该类继续其初始化步骤,同时仍具有在整个课程的其余部分与Flask进行通信的能力?/p>
Is there a sane way to force the app.run
into the background so that the class continues its initialization steps, while still having the ability to communicate with Flask throughout the rest of my class?
推荐答案
应用程序未挂起", app.run
的目的是启动一个持续运行的服务器进程,直到显式运行关闭.在启动服务器之前,应该运行所有设置逻辑.
The app isn't "hanging", the intention of app.run
is to start a persistent server process that runs until explicitly closed. You should run all setup logic before you start the server.
If you need to run tasks after the app server process has initialized, you can dispatch them in another process with something like Celery.
这篇关于阻止Flask初始化被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!