[英]Flask web application (Python 2.7)
I'm working on a flask web application and I'm new in flask. 我正在开发Flask Web应用程序,但是我是Flask中的新手。 My problem is that I want to do some things only once. 我的问题是我只想做一些事情。 For example to instantiate SearchClass and SC.refreshArray() which should be independent on refreshing my web page to increase a speed of application. 例如,实例化SearchClass和SC.refreshArray()应当独立于刷新我的网页以提高应用程序速度。 Will you give me an advice how to manage that? 您能给我建议如何处理吗? This is my code: 这是我的代码:
from flask import Flask
from flask import request
from flask import render_template
from SearchClass import *
from Database import getConnection
import pickle
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('my-form.html')
def fileSuffixArray():
with open("saf.txt", 'rb') as f:
my_list = pickle.load(f)
print(my_list)
@app.route('/', methods=['POST'])
def search():
text = request.form['text']
db = getConnection("db")
SC = SearchClass(db)
SC.refreshArray()
results=SC.getPhrase(text)
s=""
for i in range(0,len(results)):
resString=""
res=Database.searchForExactTranslation(results[i], db)
resString= ' '.join(res)
s+="<b>%s: </b> %s<br>" % (results[i], resString)
return s
app.debug = True
if __name__ == '__main__':
app.run()
Thanks in advance! 提前致谢!
Move your code to set up the database connection to a function and call the function before calling app.run() 移动代码以建立与函数的数据库连接,并在调用app.run()之前调用该函数
SC = None
def setupdb():
global SC
db = getConnection("db")
SC = SearchClass(db)
SC.refreshArray()
if __name__== "__main__":
setupdb()
app.run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.