2

I am facing the below error when I use Python Multiprocessing with Flask-SQLalchemy.

If I call the below method shown in target portion without multiprocessing, it works fine.

This is the Error Message:

Error seen in LOGS:
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
ursor.execute(statement, parameters)
psycopg2.DatabaseError: error with status PGRES_TUPLES_OK and no message from the libpq

This is the code snippet that uses multiprocessing in python

# code snippet
work = multiprocessing.Process(target=<some_method_long_running_has_db_operations_in_this>,
                               args(args1,),
                               name='PROCESS-' + self.user_key, daemon=False)

work.start()
return Response("Request Fulfilled", status=202)

Database Connection info

# This is how I use DB Session. The project contains the following files.

`db_utils.py`
# Initializing the SQLAlchemy object
db = SQLAlchemy()

Here, I again import db to use them for models.

# Flask Model classes created in the project.
from db_utils import db
class A(db.Model):
    __tablename__ = 'some_table'
    __table_args__ = {'schema': db_schema}

    id = db.Column(db.Text, primary_key=True, server_default=db.FetchedValue())
    col1 = db.Column(db.Text)
    col2 = db.Column(db.Integer)

In the business logic files: This is how I write to DB

Businesslogic.py

# Here, in this class, I again import db to call db.session etc
from db_utils import db

db.session.add(some_obj)
db.session.commit()
Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73

0 Answers0