Python 3.x 使用flask sqlalchemy进行多处理-psycopg2.DatabaseError:状态为PGRES_TUPLES_OK且没有来自libpq的消息时出错

Python 3.x 使用flask sqlalchemy进行多处理-psycopg2.DatabaseError:状态为PGRES_TUPLES_OK且没有来自libpq的消息时出错,python-3.x,multiprocessing,flask-sqlalchemy,flask-restplus,Python 3.x,Multiprocessing,Flask Sqlalchemy,Flask Restplus,我在尝试将多处理与Alchemy结合使用时遇到以下异常 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/loca

我在尝试将多处理与Alchemy结合使用时遇到以下异常

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)
worker=multiprocessing.Process(target=,args=(data,),name='Process\u ID',daemon=False)
worker.start()
返回响应(“请求已接受”,状态=202)
我在SO()中看到了类似问题的答案,它建议使用engine.dispose(),但在我的例子中,我直接使用db.session,而不是手动创建引擎和作用域


请帮助解决这个问题。谢谢

我也有同样的问题。跟踪Sam的链接帮助我解决了这个问题

在我之前(没有工作):

这对我很有用:

from multiprocessing import get_context
with get_context("spawn").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, ...])