Python Postgres Connections with Green Threads

huangapple go评论70阅读模式
英文:

Python Postgres Connections with Green Threads

问题

我正在使用psycopg2,并且在我的应用程序中有多个绿色线程。每个线程都是使用psycopg2.connect获取连接。有时我会遇到以下错误:

error: Second simultaneous read on fileno 14 detected.  Unless you really know what you're doing,
make sure that only one greenthread can read any particular socket.  Consider using a pools.Pool.
If you do know what you're doing and want to disable this error, call
eventlet.debug.hub_prevent_multiple_readers(False) - MY THREAD=<built-in method switch of
GreenThread object at 0x7fbf6aafc048>; THAT THREAD=FdListener('read', 14,
<built-in method switch of greenlet.greenlet object at 0x7fbf6aafc470>,
<built-in method throw of greenlet.greenlet object at 0x7fbf6aafc470>)

据我所知,我在这个项目中没有配置连接池。(grep -ri pool .; 返回空结果。)

psycopg2.connect是否会在某种隐式连接池中重复使用连接?

我如何获取一个新的连接而不重用旧连接(或套接字)?

英文:

I am using psycopg2 and I have more than one green thread in my application. Each thread gets connections using psycopg2.connect. Sometimes I get the following error:

error: Second simultaneous read on fileno 14 detected.  Unless you really know what you&#39;re doing,
make sure that only one greenthread can read any particular socket.  Consider using a pools.Pool.
If you do know what you&#39;re doing and want to disable this error, call
eventlet.debug.hub_prevent_multiple_readers(False) - MY THREAD=&lt;built-in method switch of
GreenThread object at 0x7fbf6aafc048&gt;; THAT THREAD=FdListener(&#39;read&#39;, 14,
&lt;built-in method switch of greenlet.greenlet object at 0x7fbf6aafc470&gt;,
&lt;built-in method throw of greenlet.greenlet object at 0x7fbf6aafc470&gt;)

I don't have connection pooling configured in this project as far as I know. (grep -ri pool .; returns nothing.)

Does psycopg2.connect reuse connections in some sort of implicit connection pool?

How do I get a new connection without reusing old connections (or sockets)?

答案1

得分: 1

Psycopg2 默认情况下在调用 psycopg2.connect() 时会重用与数据库的连接,这可能会导致在多个协程同时使用同一连接时出现错误。

为了避免这种情况,您可以使用连接池,例如 SQLAlchemy 或内置的 psycopg2.pool

或者,您可以每次需要连接到数据库时创建一个新的数据库连接。

import psycopg2
conn = psycopg2.connect(database="mydatabase", user="myusername", password="pass", host="myhost", port="myport", connection_factory=None)

请注意,这种方法可能会较慢且效率低下。

个人建议:使用连接池。

英文:

Psycopg2 by default resuses connection to the DB when you call psycopg2.connect(). many times, so if multiple green threads use the same connection at the same time, you can get this error.

To avoid this, use a connection pool, such as SQLAlchemy orbuilt-in psycopg2.pool.

Alternatively, you can create a new connection to the DB every time you need to connect to DB.

import psycopg2
conn = psycopg2.connect(database=&quot;mydatabase&quot;, user=&quot;myusername&quot;, password=&quot;pass&quot;, host=&quot;myhost&quot;, port=&quot;myport&quot;, connection_factory=None)

Note, that this can be slow and very inefficient.

Personal advice: use a connection pool.

huangapple
  • 本文由 发表于 2023年2月10日 05:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定