-
-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connection Sharing With psycopg2.connect()? #1725
Comments
If you are using greenlets then you should use psycopg in green mode. You should also make sure that you are not sharing connections across processes, which I don't know if it's happening or not in flask. No, psycopg will not reuse a connection across threads. Looking better, I see that you are using eventlet. Did you call the appropriate functions to make eventlet compatible with psycopg? |
Hey, your name is on that page 🚀! The first two lines of my main
Thank you for drawing my attention to this. I wasn't aware that this project uses that facility, nor that I use greenthreads by default because of this.
Since my project uses
Since my project uses The problem is still unexplained, but I am understanding aspects of the problem much better now! I'll look into "Migrating off of Eventlet" https://eventlet.readthedocs.io/en/latest/asyncio/migration.html#migration-guide, but this might be quite difficult since any system function call could be affected by removing that |
Does psycopg2.extensions.get_wait_callback return you |
|
It's a function, you have to call it, with |
Oh, I thought the presence of
which also appears to be a function provided by eventlet.
obviously because I'm calling the wait callback incorrectly. |
Ok, so that means that psycopg is monkeypatched well by eventlet. I have no idea what is going wrong there: you should hear from the eventlet folks. |
Thank you for your feedback! In the above logging just after:
What do you make of those file numbers? I guess it looks like psycopg opened a connection using file descriptor 14, then the connection was closed, then a new connection was opened which reused file number 14, then it was closed. This happened a dozen times or so, then a new connection was made while file number 14 was open, so file descriptor 15 was used. While that connection was open, a new connection was made resulting in file descriptor 16. Does that sound like a reasonable interpretation of this logging output? |
Describe the bug
Please let us know:
1: what you did
I start a new thread using
thread = threading.Thread(target=thread_runner); thread.start()
. Both threads (the new one and the existing one) use the following function to create a connection to the database:Then the cursor is retrieved (
cursor=conn.cursor()
) and used to execute queries (db.execute('update ftp_settings set last_submission = %s', [date]); db.connection.commit()
).2: what you expected to happen
Both thread should be able to access the database and run queries without trouble.
3: what happened instead
Logging output from the logging statement above looks like this:
After a while, I get the following error:
Is it possible that
psycopg2.connect
is returning a reused connection? Based on the error, it sounds like one greenthread used the connection, then later the second greenthread picked up the used connection. My impression is thatpsycopg2.connect
should return a new connection. Here is my python dependency list:I don't think any of these packages would provide connection pooling, but I'm not familiar with many of these packages.
If possible, provide a script reproducing the issue.
The issue isn't reliably reproducible.
The text was updated successfully, but these errors were encountered: