Acouchbase Bucket Object in a Quart App


#1

Hi all, I am trying to create a quart version of a flask app to benefit from async module. Quart version seems to be performing slower than the flask version. Below is my question as it appears on StackOverflow.

I am trying to convert a Flask app to a Quart app to add async module and gain some performance as discussed in this article. For this, I am connecting to a Couchbase bucket using the acouchbase.Bucket object. The problem is quart is slower and it crashes during the load test. Here is the code I am trying:

import asyncio
from quart import Quart, jsonify, g
from quart_openapi import Pint, Resource
import couchbase.experimental
couchbase.experimental.enable()
from acouchbase.bucket import Bucket

app = Pint(__name__, title = 'SomeTitle')

async def get_db():
    """
    Helper function to initialize and retrive the Bucket object if not 
    already present.
    """
    if not hasattr(g, 'cb_bucket'):
        g.cb_bucket = Bucket('couchbase://localhost/bucketname', 'username', 'password')
        await g.cb_bucket.connect()
    return g.cb_bucket

@app.route("/apiname/<string:xId>")
class apiname(Resource):
    async def get(self, xId):  
        cb = await get_db()
        pickle_in = open('mlmodel', 'rb')
        model = pickle.load(pickle_in)
        y_pred_proba = model.predict_proba(Member).tolist()
    return jsonify(y_pred_proba)

if __name__ == '__main__':  
app.run(port = 5000, debug = True)

App compiles without a problem though during the load test it performs bad and crashes after a while. A very similar flask app (with out any async module) is faster than the quart app but you would expect quart with async module to be faster than the flask app:

Flask equivalent looks like this:

from flask import Flask, jsonify  
from flask_restplus import Api, Resource
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
# Coucbase connections
cluster = Cluster('couchbase://localhost/')
authenticator = PasswordAuthenticator('username', 'password')
cluster.authenticate(authenticator)
cb = cluster.open_bucket('bucketname')

app = Flask(__name__) 
api = Api(app=app)

@api.route("/apiname/<string:xId>")
class apiname(Resource):
    def get(self, xId):
        Member = cb.get(xId)
        pickle_in = open('mlmodel', 'rb')
        model = pickle.load(pickle_in)
        y_pred_proba = model.predict_proba(Member).tolist()
    return jsonify(y_pred_proba)


if __name__ == '__main__':  
    app.run(port = 5000, debug = True)

Here is a comparison of quart app vs flask app. Flask looks like 10 times faster than the Quart app, which stops after the test with this error _ 96930 segmentation fault_.

Quart: Quart APP
Flask: Flask

Any answer/recommendation appreciated.