Skip to main content
 首页 » 编程设计

python之如何从 Flask (Quart) 应用程序建立到 couchbase 服务器的异步连接

2024年11月24日19dyllove98

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

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) 

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

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 应用程序的比较。 Flask 看起来比 Quart 应用程序快 10 倍,后者在测试后停止并出现此错误 _96930 segmentation fault_。

quartz : flask :

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

请您参考如下方法:

我认为你在混合 Bucket与 Cluster .

来自CouchBase documentation :

To connect to a Couchbase bucket, you must use Couchbase Role-Based Access Control (RBAC). This is fully described in the section Authorization. An authenticator, containing username and password, should be defined, and then passed to the cluster. Following successful authentication, the bucket can be opened.

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