【问题标题】:How to make a an async connection to couchbase server from Flask (Quart) app?如何从 Flask(Quart)应用程序与 couchbase 服务器建立异步连接?
【发布时间】:2019-02-14 17:36:08
【问题描述】:

我正在尝试将 Flask 应用程序转换为 Quart 应用程序以添加异步模块并获得一些性能,如 article 中所述。为此,我使用 acouchbase.Bucket 对象连接到 Couchbase 存储桶。问题是夸脱速度较慢,并且在负载测试期间崩溃。这是我正在尝试的代码:

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)

应用程序编译没有问题,但在负载测试期间它表现不佳并在一段时间后崩溃。一个非常相似的烧瓶应用程序(没有任何异步模块)比 quart 应用程序要快,但您会期望带有异步模块的 quart 比烧瓶应用程序快:

Flask 等效项如下所示:

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)

这是 quart 应用程序与烧瓶应用程序的比较。 Flask 看起来比 Quart 应用程序快 10 倍,后者在测试后停止并出现此错误_96930 分段错误_。

夸脱: 烧瓶:

任何答案/建议表示赞赏。

【问题讨论】:

  • 想知道正在使用哪种基准/测试工具?
  • 贴图来自wrk。我们还用 gatling 对其进行了测试。

标签: python asynchronous couchbase python-asyncio quart


【解决方案1】:

我认为您将BucketCluster 混合在一起。

来自CouchBase documentation

要连接到 Couchbase 存储桶,您必须使用基于角色的 Couchbase 访问控制 (RBAC)。这在本节中有完整描述 授权。一个验证器,包含用户名和密码, 应该定义,然后传递给集群。下列的 认证成功,可以打开bucket。

from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
cluster = Cluster('couchbase://localhost')
authenticator = PasswordAuthenticator('username', 'password')
cluster.authenticate(authenticator)
bucket = cluster.open_bucket('bucket-name')

所以,对于你的情况,它应该是这样的:

if not hasattr(g, 'cb_bucket'):
  cluster = Cluster('couchbase://localhost')
  authenticator = PasswordAuthenticator('username', 'password')
  cluster.authenticate(authenticator)
  bucket = await cluster.open_bucket('bucket-name')
return bucket

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2018-02-13
  • 1970-01-01
相关资源
最近更新 更多