许多线程中的 Postgres 连接

Postgres connections in many threads

在特殊情况下我需要建议。

我有这样一个程序:

data = [...]
multithread.Pool(n, data)

def slow_function(data)
    db = psycopg2.connect(credentials)
    cursor = db.cursor()
    new_data = realy_slow_func()
    some_query = "some update query"
    cursor.execute(some_query )
   

在每个线程中打开新连接安全吗?它是否慢并不重要,并且存在更快的方法。

您也应该使用 connection pool, which will create a pool of connections and reuse the same connections across your thread. I would suggest using a ThreadPool,以便一次线程数 运行 等于数据库连接池中可用的连接数。但是对于这个问题的范围,我会说DB Connection Pool

我没有测试代码,但它应该是这样的。您首先创建一个 connectionPool,然后在您的线程中从中获取连接,并在完成后释放连接。您还可以在线程外部管理获取连接和释放,只需将连接作为参数传递,并在线程完成后释放

突出显示 ThreadedConnectionPool,因为 class 用于创建池,顾名思义,它与线程一起工作。

来自文档:

A connection pool that works with the threading module. Note This pool class can be safely used in multi-threaded applications.

import psycopg2
from psycopg2 import pool

postgreSQL_pool = psycopg2.pool.ThreadedConnectionPool(1, 20, user="postgres",
                                                         password="pass@#29",
                                                         host="127.0.0.1",
                                                         port="5432",
                                                         database="postgres_db")

data = [...]
multithread.Pool(n, data)

def slow_function(data):
    db = postgreSQL_pool.getconn()
    cursor = db.cursor()
    new_data = realy_slow_func()
    some_query = "some update query"
    cursor.execute(some_query)
    cursor.close()
    postgreSQL_pool.putconn(db)

来源:https://pynative.com/psycopg2-python-postgresql-connection-pooling/

文档:https://www.psycopg.org/docs/pool.html