Flask Cloudant slow response time

303 Views Asked by At

I am creating a Flask application that is connecting to a Cloudant database using the python cloudant library.

My response time when I just add connect statement (with no queries) can be anywhere from .4s to 12s. My connect statement is like so:

client = Cloudant(USERNAME, PASSWORD, url=URL, connect=True)

When I remove the connection code, my response time is very low.

I have run a profiler on my system and it shows that the increase in response time is due to reading an ssl socket.

I have also tried using the default example from IBM Bluemix Github and got similar results for response time.

I am running my Flask application using the built in development web server. I have tried connecting to the database before every request and I have tried having a single connection that gets reused. Could this delay be due to my local machine? And what would cause it to be quick some times and not others? Other posts have suggested issues with IPv6 or DNS, but I do not think that is the case.

With API calls like:

ddoc = DesignDocument(g.db, '_design/docs')
g.myview = View(ddoc, 'my-view')
g.myview(key=[somekey])['rows']

I have already created the views and are indexed by the appropriate key, so it is not slow due to indexing.

1

There are 1 best solutions below

1
On

try to use this code to connect to your Cloudant database:

def conn(user, pwd, db, **kwargs):
    client = Cloudant(user, pwd, account=kwargs.get('host', user))
    client.connect()
    database = self.client[db]