发出两个请求时变量(列表)不持久 - FLASK

发布于 2025-01-09 19:30:14 字数 1000 浏览 0 评论 0原文

我在构建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2025-01-16 19:30:14

将 dict 初始化放在 a_route 函数之外应该可以解决这个问题。

put dict initiation out of a_route function should solve this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文