7

I have multiple worker processing job from a queue, and during the processing each worker opens a connection without closing it. Is there any consequence in doing so?

For example

conn = psycopg2.connect(dbname="my_db_name",
    user="my_user",
    password="my_password",
    host="my_host",
    port="5432")

cur = conn.cursor()
cur.execute("SELECT * FROM test;")
cur.fetchone()
conn.commit()
# What happen if I omit the line below for each job?
# cur.close()
# conn.close()
Jal
  • 2,174
  • 1
  • 18
  • 37
  • 3
    Possible duplicate of [Is it required to close a Psycopg2 connection at the end of a script?](https://stackoverflow.com/questions/31240830/is-it-required-to-close-a-psycopg2-connection-at-the-end-of-a-script), stressing the word "possible". – Ilja Everilä Jan 23 '18 at 08:21
  • 1
    In short: when the `conn` object goes out of scope, and if no other references to it exist, [it will be closed](http://initd.org/psycopg/docs/connection.html#connection.close). The exact timing depends on your Python implementation. – Ilja Everilä Jan 23 '18 at 08:23
  • You may observe idle (or idle in transaction if you don't commit) connections. – moooeeeep Jan 23 '18 at 08:24

0 Answers0