Python连接池创建失败

【TDengine 使用环境】
生产环境 3.3.6.9

【TDengine 版本】
3.3.6.9

【操作系统以及版本】
windows

python使用连接池连接TD数据库

【问题复现路径/shan】做过哪些操作出现的问题
1、安装相应python包
pip3 install taos-ws-py
pip install sqlalchemy

各个包版本:
greenlet 3.2.4
pip 25.1
setuptools 78.1.1
SQLAlchemy 2.0.43
taos-ws-py 0.6.1
typing_extensions 4.14.1
wheel 0.45.1

2、下载连接池示例代码
[建立连接 | TDengine 文档 | 涛思数据]
https://docs.taosdata.com/develop/connect/#%E8%BF%9E%E6%8E%A5%E6%B1%A0

import taosws
from sqlalchemy import create_engine
from sqlalchemy import text
import threading

# Create a SQLAlchemy engine with WebSocket connection
# If using native connection, use `taos` instead of `taosws`
engine = create_engine(url="taosws://root:taosdata@localhost:6041?timezone=Asia/Shanghai", pool_size=10, max_overflow=20)

def init_db():
    try:
        with engine.begin() as conn:
            # create database
            conn.execute(text("DROP DATABASE IF EXISTS power"))
            conn.execute(text("CREATE DATABASE IF NOT EXISTS power"))
            print(f"Create database power successfully")

            # create super table
            conn.execute(
                text("CREATE TABLE IF NOT EXISTS power.meters (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(64))")
            )
            print(f"Create stable power.meters successfully")

    except Exception as err:
        print(f"Failed to create db and table; ErrMessage:{err}")
        raise

def ws_insert_sql(i: int):
    try:
        with engine.begin() as conn:
            sql = text(f"""
                INSERT INTO 
                power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco')
                    VALUES (NOW + {i+1}a, 10.30000, 219, 0.31000) 
                    (NOW + {i+2}a, 12.60000, 218, 0.33000) (NOW + {i+3}a, 12.30000, 221, 0.31000)
                power.d1002 USING power.meters (groupid, location)  TAGS(3, 'California.SanFrancisco') 
                    VALUES (NOW + {i+1}a, 10.30000, 218, 0.25000)
                """)
            affectedRows = conn.execute(sql)
            print(f"Successfully inserted {affectedRows} rows to power.meters.")

    except Exception as err:
        print(f"Failed to insert data to power.meters; ErrMessage:{err}")
        raise

# Use connection pool to execute queries
def ws_query(sql: str):
    try:
        # Get connection from pool
        with engine.begin() as conn:
            # Execute SQL
            result = conn.execute(text(sql))
            # Get results
            data = result.fetchall()
            print(f"Query result: {data}")
            return data
    except Exception as e:
        print(f"TDengine query failed: {e}")
        raise

if __name__ == "__main__":
    init_db()  # Initialize database and tables
    threads = []
    for i in range(5):
        t1 = threading.Thread(target=ws_insert_sql, args=(i*10,))
        t2 = threading.Thread(target=ws_query, args=("SELECT * FROM power.meters",))
        threads.extend([t1, t2])
        t1.start()
        t2.start()

    for t in threads:
        t.join()

    data = ws_query("SELECT count(*) FROM power.meters")
    assert data[0][0] == 20, "Expected 20 rows in power.meters"
    print("All sub-threads completed, main thread ending")
    

3、修改相应配置
engine = create_engine(url=“taosws://root:taosdata@localhost:6041?timezone=Asia/Shanghai”, pool_size=10, max_overflow=20)
改为自己的td数据库地址

【遇到的问题:问题现象及影响】

Traceback (most recent call last):
  File "D:\lisiner\lisiner_td_to_maxcomputer\td_to_maxcomputer_local\test.py", line 10, in <module>
    engine = create_engine("taosws://ro30t:ppppP@161.476.101.377:6041/taosr")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in create_engine
  File "C:\Users\linta\.conda\envs\td_to_maxcomputer\Lib\site-packages\sqlalchemy\util\deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\linta\.conda\envs\td_to_maxcomputer\Lib\site-packages\sqlalchemy\engine\create.py", line 568, in create_engine
    entrypoint = u._get_entrypoint()
                 ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\linta\.conda\envs\td_to_maxcomputer\Lib\site-packages\sqlalchemy\engine\url.py", line 772, in _get_entrypoint
    cls = registry.load(name)
          ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\linta\.conda\envs\td_to_maxcomputer\Lib\site-packages\sqlalchemy\util\langhelpers.py", line 453, in load
    raise exc.NoSuchModuleError(
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:taosws

请问这个是什么原因呢?

可以微信具体看下 a15652223354

确定安装了连接器和客户端?

你这个客户端在哪下的

请问解决了吗,我遇到了同样的问题

需要安装 taospy pip3 install taospy

此话题已在最后回复的 30 天后被自动关闭。不再允许新回复。