在mod-wsgi中使用Python Flask-restful [英] Using Python Flask-restful with mod-wsgi

查看:75
本文介绍了在mod-wsgi中使用Python Flask-restful的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Apache 2.2上使用mod-wsgi

I am trying to use mod-wsgi with Apache 2.2

我具有以下目录结构:

scheduling-algos
-lib
-common
 -config
  -config.json
resources
-Optimization.py
optimization.wsgi
optimization_app.py

我的optimization_app.py如下:

from flask import Flask
from flask_restful import Api
from resources.Optimization import OptimizationAlgo

def optimizeInstances():
    optimization_app = Flask(__name__)
    api = Api(optimization_app)
    api.add_resource(OptimizationAlgo, '/instances')

if __name__ == '__main__':
    optimizeInstances()
    optimization_app.run(host='0.0.0.0', debug=True)

我的Optimization.py代码如下所示:

class OptimizationAlgo(Resource):
    def post(self):
       return "success"

当我对URL http://<host>:5000/instances发出POST请求时,它按预期方式工作.我想使用WSGI进行这项工作.我已经在Apache 2.2上安装了mod-wsgi.

When I make a POST request to the url http://<host>:5000/instances, it works just as expected. I want make this work using WSGI. I have mod-wsgi installed with Apache 2.2.

我的optimization.wsgi文件如下所示

import sys
sys.path.insert(0, '<path to app>')

from optimization_app import optimizeInstances as application

我收到以下错误:TypeError: optimizeInstances() takes no arguments (2 given).显然,这不是使用WSGI的正确方法.使用WSGI的正确方法是什么? 显然,这不是使用WSGI的正确方法.

I get the following error: TypeError: optimizeInstances() takes no arguments (2 given) . Apparently this is not the correct way to use WSGI. What is the correct way to use WSGI? Apparently, this is not the correct way to use WSGI.

推荐答案

正如我在另一个问题中告诉您的那样,您也许应该回过头来阅读Flask 文档.这样,您将正确学习和理解.通过忽略建议并期望其他人告诉您,这只会使人们感到烦恼,他们将停止帮助您.建议您注意这一点,而不要留下一些单独的问题,希望有人会为您解决您的问题.

As I told you in your other question, you should perhaps go back and read the Flask documentation again. That way you will learn and understand properly. By ignoring advice and expecting others to tell you, it only annoys people and they will stop helping you. Would suggest you take heed of that rather than leave a trail of separate questions hoping someone will solve your problems for you.

也就是说,我看不出您所提供的代码如何甚至可以与Flask开发服务器一起使用.问题在于optimization_app = Flask(__name__)在函数范围内设置了局部变量.它没有设置全局变量.结果,optimization_app.run(host='0.0.0.0', debug=True)的调用应以LookupError失败,因为它将看不到名为optimization_app的变量.甚至不知道为什么要烦恼该功能.

That said, I can't see how the code you give can even work with the Flask development server as you claim. The problem is that optimization_app = Flask(__name__) is setting a local variable within function scope. It isn't setting a global variable. As a result the call of optimization_app.run(host='0.0.0.0', debug=True) should fail with a LookupError as it will not see a variable called optimization_app. Not even sure why you are bothering with the function.

如果您查看Flask文档,它可能会使用的模式是:

If you go look at the Flask documentation, the pattern it would likely use is:

# optimisation.wsgi

import sys
sys.path.insert(0, '<path to app>')

# We alias 'app' to 'application' here as mod_wsgi expects it to be called 'application'.

from optimization_app import app as application

# optimization_app.py

from flask import Flask
from flask_restful import Api
from resources.Optimization import OptimizationAlgo

app = Flask(__name__)

api = Api(app)
api.add_resource(OptimizationAlgo, '/instances')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

这篇关于在mod-wsgi中使用Python Flask-restful的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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