python web应用程序中的全局变量(flask)
我正在运行我的问题后面的python web应用程序。当我从命令行运行它时,一切正常。它调用程序的main部分加载xml文件,随后从/search/term路径发出的任何请求my web app都会返回正确的值。但是,如果我从apache运行它,则自然不会执行main。在这种情况下,我如何打开文件一次,然后能够在每次请求时对其进行查询?这是可能的,还是我应该将数据加载到数据库以获得这种持久性?理想情况下,我希望避免db调用的复杂性,因为我正在执行简单的字符串搜索操作。 提前感谢(代码如下) 马里内罗python web应用程序中的全局变量(flask),python,web-applications,flask,Python,Web Applications,Flask,我正在运行我的问题后面的python web应用程序。当我从命令行运行它时,一切正常。它调用程序的main部分加载xml文件,随后从/search/term路径发出的任何请求my web app都会返回正确的值。但是,如果我从apache运行它,则自然不会执行main。在这种情况下,我如何打开文件一次,然后能够在每次请求时对其进行查询?这是可能的,还是我应该将数据加载到数据库以获得这种持久性?理想情况下,我希望避免db调用的复杂性,因为我正在执行简单的字符串搜索操作。 提前感谢(代码如下) 马里
from flask import Flask
from classes import History
from xmlReader import xmlReader
app = Flask(__name__)
@app.route('/')
def index():
return '<h1>Hello World!</h1>'
@app.route('/search/<term>')
def user(term):
(resultCoords, resultTerms) = historyObj.searchForTerm(term)
result = "<p>"
for t in resultTerms:
result = result + t.string + '<br>'
result = result + '</p>'
return result
if __name__ == '__main__':
reader = xmlReader("web_history.xml")
global historyList
historyList = reader.getData()
global historyObj
historyObj = History(historyList)
app.run()
从烧瓶导入烧瓶
从类导入历史
从xmlReader导入xmlReader
app=烧瓶(名称)
@应用程序路径(“/”)
def index():
返回“你好,世界!”
@app.route(“/search/”)
def用户(术语):
(resultwords,resultTerms)=historyObj.searchForTerm(术语)
结果=“”
对于ResulterMS中的t:
result=result+t.string+'
'
结果=结果+''
返回结果
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
reader=xmlReader(“web_history.xml”)
全球历史学者
historyList=reader.getData()
全球历史OBJ
historyObj=历史(历史列表)
app.run()
当然,问题是当在Apache中的request
上下文中时,app上下文中存储的变量将不可用
以下是Miguel Grinberg关于在请求
上下文中全局提供变量的优秀著作中的代码片段:
@main.app_context_processor
def inject_permissions():
return dict(Permission=Permission)
所以在你的情况下
@app.app_context_context_processor
def inject_permissions():
#return globals here
然后,您应该能够在请求上下文中访问应用上下文中可用的文件
编辑:将发表评论,但没有足够的代表
有经验的人可能会更好地回答我们的解决方案之间的差异,但我会试一试
我的实现和你的实现之间唯一明显的区别是,无论Flask应用程序的上下文如何,代码都会自动运行。这在app\u上下文中显然是不必要的
一旦应用程序变得更复杂(蓝图、不同的配置),也可能会出现其他问题,因为变量是在定义之前定义的,所以我认为修改全局变量将很困难
编辑2:对第二条评论的回应:我再次看了这本书,是的,这就是他使用的模式背后的原因:
为了避免在每次render_template()调用中添加模板参数,可以使用context>处理器。上下文处理器使变量对所有模板全局可用
回答我自己的问题@福克斯洛克,我应该更喜欢你的方法吗?有什么原因吗?非常感谢。对我有效的解决方案是:
from flask import Flask
from classes import History
from xmlReader import xmlReader
from flask.ext.script import Manager
app = Flask(__name__)
manager = Manager(app)
globalString = "hello"
reader = xmlReader("web_history.xml")
global historyList
historyList = reader.getData()
global historyObj
historyObj = History(historyList)
@app.route('/')
def index():
return '<h1>Hello World!</h1>'
@app.route('/search/<term>')
def user(term):
(resultCoords, resultTerms) = historyObj.searchForTerm(term)
result = "<p>"
for t in resultTerms:
result = result + t.string + '<br>'
result = result + '</p>'
return result
if __name__ == '__main__':
manager.run()
从烧瓶导入烧瓶
从类导入历史
从xmlReader导入xmlReader
从flask.ext.script导入管理器
app=烧瓶(名称)
经理=经理(应用程序)
globalString=“你好”
reader=xmlReader(“web_history.xml”)
全球历史学者
historyList=reader.getData()
全球历史OBJ
historyObj=历史(历史列表)
@应用程序路径(“/”)
def index():
返回“你好,世界!”
@app.route(“/search/”)
def用户(术语):
(resultwords,resultTerms)=historyObj.searchForTerm(术语)
结果=“”
对于ResulterMS中的t:
result=result+t.string+'
'
结果=结果+''
返回结果
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
manager.run()
Noob问题:为什么您不能从if\uuu name\uuuu
块中删除所有其他语句,并且在if块中只有app.run()
?感谢您的回复。在看到你回复之前,我只是设法让它起作用。我的解决方案在哪方面比你的更糟?我的应用程序看起来更简单,但我又是一个傻瓜。->我的应用程序现在有些慢了(在apache上)。您认为我的解决方案会在每次请求时打开文件吗?这违背了目的,可能会使您的解决方案更好。你觉得怎么样?谢谢@FoxRocks的时间和清晰的回答:)