发出两个请求时变量(列表)不持久 - FLASK
我在构建 Flask 应用程序时遇到问题。我有一个名为“routes.py”的模块,其中写有所有路线。然后我有另一个名为“functions.py”的模块,其中有一些与路线相关的函数。问题是,当我向一条路由发出特定请求时,我想将一些数据附加到列表中,所有这些数据都保存在“functions.py”模块中,该模块在每个路由中都会被调用。所以问题是,每次我发出请求时,列表都会重置为 0 并且为空。我怎样才能保留该列表?我尝试在另一个模块中创建一个列表并保留它,但没有成功。我在这里给你一个有问题的模式。谢谢!
routes.py
@bp.route('/a_route', methods=['POST'])
def a_route():
try:
dict={
"some_data": "some_data"
}
p = Process(target=a_function, args=(dict,))
p.daemon = True
p.start()
except Exception as err:
return make_response(jsonify({
"err": True
}), 500)
else:
return make_response(jsonify({
"err": False
}), 200)
上面的函数调用functions.py中的“a_function”。
函数.py
#receives some data
# and appends that data to a list (this list is empty every time I call a function, that´s de problem
list.append(dict['some_data']
I´m having problems building a Flask application. I have a module called 'routes.py' where I have all my routes written. And then I have another module called 'functions.py' where I have some functions related to the routes. The thing is, that when I make an specific request to one route, I want to append some data to a list, all this being saved in the 'functions.py' module, which is called in every route. So the problem is, that every time I make a request, the list resets to 0 and its empty. How can I persist that list? I´ve tried creating a list in another module and persisting it but hasn´t worked. I let you here a schema with the problem. Thanks!
routes.py
@bp.route('/a_route', methods=['POST'])
def a_route():
try:
dict={
"some_data": "some_data"
}
p = Process(target=a_function, args=(dict,))
p.daemon = True
p.start()
except Exception as err:
return make_response(jsonify({
"err": True
}), 500)
else:
return make_response(jsonify({
"err": False
}), 200)
The above function calls 'a_function' that is in functions.py.
functions.py
#receives some data
# and appends that data to a list (this list is empty every time I call a function, that´s de problem
list.append(dict['some_data']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 dict 初始化放在 a_route 函数之外应该可以解决这个问题。
put dict initiation out of a_route function should solve this problem.