我有一个功能可以在网络上抓取数据并计算搜索分数。但是,这可能需要一段时间,有时网页会在完成执行之前超时。
因此,我创建了一个单独的线程来执行该函数并loading.html告诉客户端仍在收集数据。一旦函数在线程中结束,我如何重新加载网页以显示output.html显示分数。
这是我迄今为止所拥有的一个更简单的版本:
from flask import Flask
from flask import render_template
from threading import Thread
app = Flask(__name__)
@app.route("/")
def init():
return render_template('index.html')
@app.route("/", methods=['POST'])
def load():
th = Thread(target=something, args=())
th.start()
return render_template('loading.html')
def something():
#do some calculation and return the needed value
if __name__ == "__main__":
app.run()
Run Code Online (Sandbox Code Playgroud)
如何 在线程完成render_template('output.html', x=score) 后将我的应用程序路由到一次?something()th
我试图避免像 redis 这样的任务队列,因为我想在网络上部署这个应用程序,我不想产生费用(这更像是一种实验和爱好)。
详细的代码答案会很有帮助,因为我是烧瓶和多线程的新手
这是我的一段代码.
int** n;
for (int i = 0; i < N; ++i){
int size;
cin >> size;
int* n[i] = new int[size]; //line 13
for (int j = 0; j < size; ++j){
cin >> n[i][j];
}
}
Run Code Online (Sandbox Code Playgroud)
这是编译期间弹出的错误.
solution.cc:在函数'int main()'中:
solution.cc:13:33:error:数组必须用括号括起的初始值设定项初始化
int*n [i] = new int [size];
我意识到这可以使用向量来完成,因为它们在动态处理内存方面要好得多.但我不允许使用向量来应对这一挑战.我被允许使用的唯一标题是"iostream".
我读到,在C++ 11中,语法已被修改为new:
p = new T [N] {initializer1, ..., initializerN};
Run Code Online (Sandbox Code Playgroud)
资料来源:https://en.wikipedia.org/wiki/New_and_delete_(C%2B%2B)#Overview
但我的代码仍然会为类似的变体产生相同的错误.
有没有办法完全绕过初始化错误?如果不是,则假设将值初始化为优选地相同的值.