Use sqlAlchemy instead of psycopg2 #1698
Replies: 1 comment 7 replies
-
I don't have a lot of experience using SQLAlchemy in the way you describe, but it is a fully supported way to use the library, so I don't see any problems on that part of your plans. The problem that I see is that you have an assumption that SQLAlchemy can magically handle concurrency, which is not true. Regardless of the method you use to access the database, you will need to create multiple connections if you intend to have concurrent queries. From your description it seems you are creating a single connection and then you share it. You will have the same error with SQLAlchemy if you use this same design with it. So I think you have more a design problem than a library limitation problem. You should be able to do what you want with psycopg2 as well. |
Beta Was this translation helpful? Give feedback.
-
Hello,
I have a flask + socketio + postgresql server, I am using psycopg2 to send requests to my database.
At the beginning I was often confronted with the following error:
psycopg2.ProgrammingError: execute cannot be used while an asynchronous query is underway
I understood that this error occurs because of eventlet, 2 greenlet tries to use the connection to the database at the same time, which is not possible. I have corrected this problem using the eventlet semaphore. When calling a route from my API, it waits for a connection to the database to be available before executing a request.
It works well but causes slowness when my API is under a heavy workload.
By going through the discussions I understand that sqlAlchemy manages this concurrency problem natively.
But I don't want to use sqlAlchemy's ORM features, I would like still to execute sql queries like: "SELECT * FROM ...".
Is it possible to set up sqlAlchemy only to manage the connection to the database and manage the concurrency while keeping the requests in sql form? If so, do you have an example?
Beta Was this translation helpful? Give feedback.
All reactions