Python Forum
Flask, Posgresql - Multiple requests are not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Flask, Posgresql - Multiple requests are not working (/thread-24512.html)



Flask, Posgresql - Multiple requests are not working - bmaganti - Feb-17-2020

All,

First of thank you for supporting this firm.

I am building an enterprise REST web application using Flask. application connections going crazy and overlapping each other when I am running more than one request.

Here is calling pattern

api class -> Service class1 -> Service Class2 -> Data Access Class(Database connection) -> Utility class to convert dataset to map


Any suggestions?


RE: Flask, Posgresql - Multiple requests are not working - ndc85430 - Feb-17-2020

There's not enough detail there to be able to diagnose the problem you're having. Can you be more specific about what "application connections going crazy and overlapping each other" means? Can you at least provide a minimal code sample that demonstrates the issue?


RE: Flask, Posgresql - Multiple requests are not working - bmaganti - Feb-18-2020

Thank you for your reply.

The database connections are overlapping with multiple requests. Is there any specific coding style I need to follow to avoid it?


Example:
I called /foo service back to back. At some point of the time, DB calls are overlapping
this operation takes about 5 minutes and makes lot of DB calls.


Classes hirarchy or dependency
api class -> Service class1 -> Service Class2 -> Data Access Class(Database connection) -> Utility class to convert dataset to map

Sometimes I am initiating the dependency class in function, class, and sometimes in __init__


RE: Flask, Posgresql - Multiple requests are not working - bmaganti - Feb-18-2020

class DemandGenerationDataAccess(object):

    __app_config = AppConfig()

    __debug = __app_config.get_config_value('env', 'debug')

    def __init__(self):
        self._db_connection = psycopg2.connect(self.__app_config.get_connection_string())
        self._db_cur = self._db_connection.cursor()

    def get_data(self, p1, p2, p3):
        query = "query......"
        query_inputs = (p1, p2, p3)
        return self.__get_all_query_safe(query, query_inputs)

    def __get_all_query_safe(self, query, query_inputs, json_util=None):
        try:
            self.__queries.append(query)
            self._db_cur.execute(query, query_inputs)
            return self.__convert_to_json(self._db_cur.fetchall(), False)
        except psycopg2.Error as e:
            self.logger.debug("Cannot execute the query!!", e.pgerror)
            raise

    def __convert_to_json(self, data, fetch_first_row):
        items = []
        for row in data:
            d = collections.OrderedDict()
            col_counter = 0
            for key in self._db_cur.description:
                d[key[0]] = str(row[col_counter])
                col_counter += 1
            items.append(d)
            if fetch_first_row:
                break
        return items



RE: Flask, Posgresql - Multiple requests are not working - bmaganti - Feb-19-2020

Thank you, Crater. I will do that.

Any suggestions for my problem.

Why Python doesn't create a separate thread for each API request in flask? why it's overlapping?

The other classes are working as singleton even API requested is a separate thread?


RE: Flask, Posgresql - Multiple requests are not working - bmaganti - Feb-20-2020

Anyone can guide me to good connection management examples in Python/Flask?

My Db connections are overlapping each other