初始化使用Apache和mod_wsgi运行的Flask应用 [英] Initialising a Flask app running with Apache and mod_wsgi

查看:92
本文介绍了初始化使用Apache和mod_wsgi运行的Flask应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用mod_wsgi在Apache下运行的Flask应用.该应用程序需要做一些初始化,包括设置一些顶级变量,这些变量需要在接收到请求之前在请求处理程序中可以访问.目前,此初始化代码只是 app.py 中的顶级语句,位于请求处理方法之前:

I've got a Flask app running under Apache using mod_wsgi. The app needs to do do some initialisation, including setting some top-level variables that need to be accessible inside the request handlers, before it receives any requests. At the moment this initialisation code is just top-level statements in app.py before the request handling methods:

from flask import Flask, Response, request

<other app imports>

APP = Flask(__name__)

# initialisation code

@APP.route(<URL for request #1>)
def request_handler_1():
    # request handler code

@APP.route(<URL for request #2>)
def request_handler_2():
    # request handler code

应用程序的wsgi文件如下所示:

The application's wsgi file looks like this:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/myapp")

from myapp.app import APP as application
application.secret_key = <secret key>

我注意到在收到第一个请求之前,不会调用初始化代码.在接收到任何请求之前,如何通过mod_wsgi加载应用程序时执行初始化代码?

I've noticed that the initialisation code is not called until the first request is received. How can I made the initialisation code be executed when the app is loaded by mod_wsgi, before any requests are received?

推荐答案

它是在第一个请求上发生的,因为默认情况下,mod_wsgi仅在第一个请求到达时才加载WSGI脚本文件.也就是说,它会延迟加载您的WSGI应用程序.

It is happening on first request because by default mod_wsgi will only load your WSGI script file when the first request arrives. That is, it lazily loads your WSGI application.

如果您希望在进程首次启动时强制它加载WSGI应用程序,那么您需要告诉mod_wsgi这样做.

If you want to force it to load your WSGI application when the process first starts, then you need to tell mod_wsgi to do so.

如果您具有以下配置:

WSGIDaemonProcess myapp
WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /some/path/app.wsgi

将其更改为:

WSGIDaemonProcess myapp
WSGIScriptAlias / /some/path/app.wsgi process-group=myapp application-group=%{GLOBAL}

只有在 WSGIScriptAlias 上同时指定了进程组和应用程序组时,而不是使用单独的指令,mod_wsgi才可以预先知道WSGI应用程序将在哪个进程/解释器上下文中运行,以及因此,请预先加载WSGI脚本文件.

It is only when both process group and application group are specified on WSGIScriptAlias, rather than using the separate directives, that mod_wsgi can know up front what process/interpreter context the WSGI application will run in and so preload the WSGI script file.

BTW,如果尚未使用mod_wsgi守护程序模式( WSGIDaemonProcess 指令),并强制使用主解释器上下文( WSGIApplicationGroup%{GLOBAL} 指令),应该是,因为这是首选设置.

BTW, if you weren't already using mod_wsgi daemon mode (the WSGIDaemonProcess directive), and forcing the main interpreter context (the WSGIApplicationGroup %{GLOBAL} directive), you should be, as that is the preferred setup.

这篇关于初始化使用Apache和mod_wsgi运行的Flask应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆