Python Forum

Full Version: Flask, Posgresql - Multiple requests are not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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?
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__
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
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?
Anyone can guide me to good connection management examples in Python/Flask?

My Db connections are overlapping each other