简体   繁体   English

Locust 脚本被 ThreadedConnectionPool 卡住了

[英]Locust script is getting stuck with ThreadedConnectionPool

I'm trying to write a load testing script using locust in python (it uses gevent & greenlet for multithreading internally as per my understanding);我正在尝试在 python 中使用 locust 编写负载测试脚本(根据我的理解,它在内部使用 gevent 和 greenlet 进行多线程处理); but my script is getting stuck when I try to put a db connection (postgres) back to connection pool inside a thread.但是当我尝试将数据库连接(postgres)放回线程内的连接池时,我的脚本卡住了。 I have defined connection pool variable as gloabl & then trying to create & put connections back inside threads;我已将连接池变量定义为 gloabl & 然后尝试创建 & 将连接放回线程内; I have no experience in threading;我没有穿线经验; not sure if these lines marked inside ** quotes are the reason locust gets stuck -不确定在**引号内标记的这些行是否是蝗虫卡住的原因-

@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()

You need to use psycogreen to make psycopg2 gevent-friendly.您需要使用 psycogreen 来使 psycopg2 对 gevent 友好。

Something like:就像是:

import gevent
import gevent.monkey

gevent.monkey.patch_all()
import psycogreen.gevent

psycogreen.gevent.patch_psycopg()

Full example here: https://github.com/SvenskaSpel/locust-plugins/blob/master/locust_plugins/listeners.py完整示例: https://github.com/SvenskaSpel/locust-plugins/blob/master/locust_plugins/listeners.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM