gpt4 book ai didi

python-3.x - 使用 flask sqlalchemy 进行多处理 - psycopg2.DatabaseError : error with status PGRES_TUPLES_OK and no message from the libpq

转载 作者:行者123 更新时间:2023-12-05 03:59:11 24 4
gpt4 key购买 nike

在尝试将多处理与 flask sqlalchemy 结合使用时遇到异常。

sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically.
[12/Aug/2019 18:09:52] "GET /api/resources HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/SQLAlchemy-1.3.6-py3.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1244, in _execute_context
cursor, statement, parameters, context
File "/usr/local/lib/python3.7/site-packages/SQLAlchemy-1.3.6-py3.7-linux-x86_64.egg/sqlalchemy/engine/default.py", line 552, in do_execute
cursor.execute(statement, parameters)
psycopg2.DatabaseError: error with status PGRES_TUPLES_OK and no message from the libpq

没有多处理代码工作完美,但是当我如下添加多处理时,我遇到了这个问题。

worker = multiprocessing.Process(target=<target_method_which_has_business_logic_with_DB>, args=(data,), name='PROCESS_ID', daemon=False)
worker.start()
return Response("Request Accepted", status=202)

我在 SO ( https://stackoverflow.com/a/33331954/8085047) 中看到类似问题的答案,建议使用 engine.dispose(),但在我的例子中,我直接使用 db.session,而不是手动创建引擎和范围。

请帮助解决问题。谢谢!

最佳答案

我遇到了同样的问题。按照 Sam 的链接帮助我解决了问题。

在我有(不工作)之前:

from multiprocessing import Pool
with Pool() as pool:
pool.map(f, [arg1, arg2, ...])

这对我有用:

from multiprocessing import get_context
with get_context("spawn").Pool() as pool:
pool.map(f, [arg1, arg2, ...])

关于python-3.x - 使用 flask sqlalchemy 进行多处理 - psycopg2.DatabaseError : error with status PGRES_TUPLES_OK and no message from the libpq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57476304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com