1
我的目標是讓我的Flask應用程序根據子域切換數據庫。Flask WSGI subdomain dispatching在應用程序實例上導致404s
的subdomain dispatcher described in the docs是有用的,我使用something based off it
一切似乎很好地工作,但只有2個實例似乎在給定的時間,儘管有加載多個實例SubDomainDispatcher工作。其他實例發出404。
我create_app功能如下:
def create_app(subdomain):
app = Flask("proj", static_folder="../static", template_folder="../templates")
with app.app_context():
app.config.from_object('proj.config')
app.config.update(
SQLALCHEMY_DATABASE_URI = 'mysql://root:[email protected]:3306/' + subdomain + '_proj',
HOST = subdomain + app.config["HOST"],
)
from proj.models import db, Models
engine = sqlalchemy.create_engine("mysql://root:[email protected]:3306")
engine.execute("CREATE DATABASE IF NOT EXISTS "+subdomain+"_proj")
engine.execute("use "+subdomain+"_proj")
db.init_app(app)
db.create_all()
mail = Mail(app)
store = DictStore()
KVSessionExtension(store, app)
import proj.views
return app
我使用Apache與mod_wsgi的:
Apache的配置
<VirtualHost *:80>
ServerName mywebsite.com
ServerAdmin [email protected]
WSGIScriptAlias//var/www/proj/app.wsgi
WSGIDaemonProcess application user=www-data group=www-data processes=5 threads=20
<Directory /var/www/proj/proj/>
Order allow,deny
Allow from all
</Directory>
#Alias /static /var/www/FlaskApp/FlaskApp/static
#<Directory /var/www/FlaskApp/FlaskApp/static/>
# Order allow,deny
# Allow from all
#</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
WSGI文件
import sys
sys.path.insert(0, '/var/www/proj')
from subdomain import SubdomainDispatcher
from proj import create_app
application = SubdomainDispatcher(create_app)
看起來像某種競爭條件,但我不知道它會在哪裏或爲什麼會發生。我已經嘗試將進程和線程的數量減少到1,但也沒有看到工作。
任何建議是非常感謝,因爲我一直在努力這一段時間。