Skip to content
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 pool exhausted #41

Open
lukas1994 opened this issue Oct 7, 2019 · 3 comments
Open

connection pool exhausted #41

lukas1994 opened this issue Oct 7, 2019 · 3 comments

Comments

@lukas1994
Copy link

Hi,
I want to run multiple threads that work on jobs in a queue. If a job fails I want it to retry. This is my code:

pool = ThreadedConnectionPool(4, 4, connection_string)
pq = PQ(pool=pool)

try:
    pq.create()
    print("created queue table")
except:
    print("queue table already exists")

job_queue = pq["queue"]

def worker():
  while True:
    try:
      with job_queue:
        for job in job_queue:
          if job is None:
            print("job is None")
            break
          # do stuff

    except Exception as e:
        print(f"worker {current_thread()} died because: {str(e)} -- restarting...")


for i in range(4):
    Thread(target=worker).start()

However, I'm getting connection pool exhausted exceptions. My code probably throws exceptions but this ideally it would be able to retry without any issues.

I already looked at the tests but couldn't find the issue.

@malthe
Copy link
Owner

malthe commented Oct 8, 2019

This does seem rather weird. Try using psql to introspect the active connections using e.g. select * from pg_stat_activity.

@lukas1994
Copy link
Author

I ran \COPY (SELECT * from pg_stat_activity) TO 'pg_stat_activity.csv' CSV HEADER while my application was running (and the exceptions came in). Attached the output file.

pg_stat_activity.txt

@lukas1994
Copy link
Author

Are the connections returned properly to the pool if my code throws an exception?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants