所有Flask问题的cx_Oracle SessionPool根目录 [英] cx_Oracle SessionPool root of all Flask problems

查看:90
本文介绍了所有Flask问题的cx_Oracle SessionPool根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过uwsgi在Flask中创建了一个Web服务.我以为我会遵循良好的做法,并创建一个具有20个连接的SessionPool才是安全的.每次调用Web服务端点时,我都会从池中获取一个连接,最后释放它.

I created a web service in Flask over uwsgi. I thought I would follow good practice and create a SessionPool with 20 connections to be safe. Each call to a web service endpoint, I acquire a connection from the pool, and at the end I release it.

当使用Locust来测试API时,我遇到了数百次失败,其中一些较长的响应(30Mb JSON响应)接近100%.较小的有效载荷要好得多,但会出现间歇性故障.

When using Locust to swarm test the API, I was getting hundreds of failures, nearly 100% on some of the longer responses (30Mb JSON response). Smaller payloads were much better, but with intermittent failures.

当我回到不良习惯并在方法本身内创建了一个全新的连接和游标的那一刻,我所有的问题都消失了.在数千次压力测试呼叫中100%成功.

The minute I switched back to bad practice and created a brand new connection and cursor within the method itself, all my problems vanished. 100% success on 1000s of stress test calls.

我的错误多种多样.TNS错误数据包,来自池的连接数量不正确,用户取消了请求....您将其命名为它.

My errors were varied. TNS Bad Packet, incorrect number of connections from pool, request cancelled by user....you name it, it was there.

因此,我似乎无法在Flask上使用Oracle连接池,或者在Flask应用程序级别上只有一个连接(这会产生错误,不确定原因,这就是我切换到连接池的原因).

So I can't use Oracle connection pooling with flask it seems, or have a single connection at the Flask application level (this generated errors, not sure why, which is why I switched to connection pooling).

有关在flask中使用cx_Oracle创建可扩展应用程序的任何建议.

Any advice on creating scalable apps using cx_Oracle in flask.

我的原始代码是:

pool = cx_Oracle.SessionPool("user", "password", "myserver.company.net:1521/myservice", min=10, max=10, increment=0, getmode=cx_Oracle.SPOOL_ATTRVAL_WAIT, encoding="UTF-8")







def read_products_search(search=None):
    """
    This function responds to a request for /api/products
    with the complete lists of people

    :return:        json string of list of people
    """
    conn_ariel = pool.acquire()   
    cursor_ariel = conn_ariel.cursor()


    search=search.lower()
    print("product search term is: ", search)
    # Create the list of products from our data
    sql = """
        SELECT DRUG_PRODUCT_ID, PREFERRED_TRADE_NAME, PRODUCT_LINE, PRODUCT_TYPE, FLAG_PASSIVE, PRODUCT_NUMBER
        FROM DIM_DRUG_PRODUCT 
        WHERE lower(PREFERRED_TRADE_NAME) LIKE '%' || :search1 || '%' or lower(PRODUCT_LINE) LIKE '%' || :search2 || '%'  or lower(PRODUCT_NUMBER) LIKE '%' || :search3 || '%' 
        ORDER BY PREFERRED_TRADE_NAME ASC
        """
    cursor_ariel.execute(sql, {"search1":search,"search2":search, "search3":search })
    products = []




    for row in cursor_ariel.fetchall():

        r = reg(cursor_ariel, row, False)

        product = {
            "drug_product_id"           :   r.DRUG_PRODUCT_ID,
            "preferred_trade_name"      :   r.PREFERRED_TRADE_NAME,
            "product_line"              :   r.PRODUCT_LINE,
            "product_type"              :   r.PRODUCT_TYPE,
            "flag_passive"              :   r.FLAG_PASSIVE,
            "product_number"            :   r.PRODUCT_NUMBER

        }
        # logging.info("Adding Product: %r", product)
        products.append(product)

    if len(products) == 0:
        products = None

    pool.release(conn_ariel)
    return products

推荐答案

创建池时,请使用 threaded = True .

请参见如何将Python Flask与Oracle数据库.

这篇关于所有Flask问题的cx_Oracle SessionPool根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆