Locust 脚本被 ThreadedConnectionPool 卡住了

时间:2021-03-05 08:45:55

标签: python multithreading connection-pooling locust

我正在尝试使用 python 中的 locust 编写负载测试脚本(根据我的理解,它在内部使用 gevent 和 greenlet 进行多线程);但是当我尝试将数据库连接(postgres)放回线程内的连接池时,我的脚本卡住了。我已将连接池变量定义为 global & 然后尝试创建 & 将连接放回线程内;我没有线程经验;不确定这些标记在 ** 引号内的行是否是 locust 卡住的原因 -

@events.test_start.add_listener # executes one time at the start of test run
def on_test_start(**kw):
    **global t_pool**  # connection pool variable defined as global
    conn_st = config(fname = os.path.join('.', 'db'), instance = 'xxx')
    try:
        t_pool = pool.ThreadedConnectionPool(1, 100, **conn_st)
        #cur = db.cursor()
    except Exception as err:
        #db = None
        raise DatabaseError("DB Connection could not be established - %s" %(err))

@contextmanager
def get_con():
    con = t_pool.getconn()
    try:
        yield con
    finally:
        **t_pool.putconn(con)**

def send_xxx():
    ind = random.randint(0, (len(xxx_data['yyy'])-1))
    body['toeknx'] = xxx_data['yyy'][ind]
    res = requests.post(url, headers=headers, json=body)
    res.raise_for_status()
    with get_con() as con:
        cur = con.cursor()
        while True:
            cur.execute("test query")
            token = cur.fetchone()
            if token is None:
                continue
            else:
                cur.execute("another test query")
                out = cur.fetchone()
                if zzz:
                    continue
                else:
                    cur.execute("final test query")
                    out1 = cur.fetchone()
                    time_diff = out1[1] - out1[0]
                    cur.close()

1 个答案:

答案 0 :(得分:1)

您需要使用 psycogreen 使 psycopg2 gevent 友好。

类似于:

import gevent
import gevent.monkey

gevent.monkey.patch_all()
import psycogreen.gevent

psycogreen.gevent.patch_psycopg()

完整示例在此: https://github.com/SvenskaSpel/locust-plugins/blob/master/locust_plugins/listeners.py