Python Forum
Flask, Posgresql - Multiple requests are not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask, Posgresql - Multiple requests are not working
#1
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?
Reply
#2
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?
Reply
#3
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__
Reply
#4
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
Reply
#5
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?
Reply
#6
Anyone can guide me to good connection management examples in Python/Flask?

My Db connections are overlapping each other
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Flask, send_from_directory not working : solved SpongeB0B 2 1,304 Jan-26-2021, 07:02 AM
Last Post: SpongeB0B
  My flask website not working properly Aggam 2 630 Nov-03-2020, 09:53 AM
Last Post: Aggam
  requests module is not working varsh 3 1,465 Sep-10-2020, 03:53 PM
Last Post: buran
  Flask Streaming not working on IIS Windows Revencu 0 1,003 Nov-09-2019, 10:31 PM
Last Post: Revencu
  How to create Flask-Stripe Checkout and Charge for the multiple items Antares 4 3,162 Jul-05-2019, 10:20 AM
Last Post: Antares
  flask app to save images locally when deployed on heroku not working Prince_Bhatia 1 3,121 Feb-20-2019, 11:59 PM
Last Post: snippsat
  display multiple sensors on webpage python flask jinja pascale 6 2,756 Jan-29-2019, 10:10 AM
Last Post: pascale
  flask requests display data from api on webpage with javacript pascale 0 1,456 Oct-25-2018, 08:30 PM
Last Post: pascale
  Inserting multiple cookies using python requests freshtomatoes 0 3,361 Aug-16-2018, 09:25 PM
Last Post: freshtomatoes
  Uploading multiple pdfs to Flask ( Sql alchmey) with additional variables KirkmanJ 10 6,076 Jul-27-2018, 09:49 AM
Last Post: KirkmanJ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020