My program looks like this:
import sqlalchemy
engine = sqlalchemy.create_engine(DATABASE_URI)
~some code~
for group in groups:
~some code~
for x in y:
query = ~some query~
params = ~some params~
rows = engine.connect().execute(sqlalchemy.text(query), params)
This code works for two loops then it gets stuck.
After getting rows I make a df out of it which looks ok.
Checking within postgres using this query:
SELECT pid, now() - pg_stat_activity.query_start AS dusration, pg_stat_activity.query, pg_stat_activity.state
FROM pg_stat_activity
WHERE pg_stat_activity.state = 'active'
ORDER BY pg_stat_activity.query_start DESC;
I can see that there is a query stuck in active which I guess running in the back and this is what make the program stuck.
I found some posts about sessions but couldnt find a good example for how to use and I can't understand the docs very good and I am not sure is the right solution?
The query I am using is the same but with different parameters each run.
If you can share some tips, links and anything that could help me get through the problem I will be thankful.