简体   繁体   中英

Python psycopg2 Execute Hangs

I am using Python 3.x within an app hosted on Heroku with Basic PostgreSQL instance and Im using the psycopg2 library (as "lite")

Recently started hanging when I call Execute on the cursor object.

I am not sure how to troubleshoot this and would appreciate any thoughts.

Here is how I instantiate Connection:

def getConnection():
    urlparse.uses_netloc.append("postgres")
    url = urlparse.urlparse(os.environ["HEROKU_STUFF_HERE"])

        con = lite.connect(
            database=url.path[1:],
            user=url.username,
            password=url.password,
            host=url.hostname,
            port=url.port
        )

     return con

Here is how I generally execute non-return type statements:

 def ExecuteSQL(sql):
    con = DB.getConnection()
    with con:
        cur = con.cursor()
        print("In")
        cur.execute(sql)
        print("out")

The app NEVER sees the light after printing the word "In" to the console.

I've left try except blocks out intentionally so as to blow the thing up.....no dice.

pretty much doesnt matter what the SQL Statement is, I get the same result

Running the same sql in a sql client tool executes instantly.

Im also not sure how to detect if this statement even makes it into Postgresql.....

Thanks for any help you can offer

psycopg2 opened the transaction which is not closed until a commit() or rollback(). Therefore your changes were not persistent ( docs ).

def ExecuteSQL(sql):
  con = DB.getConnection()
  with con:
    cur = con.cursor()
    cur.execute(sql)
    con.commit()

or you could just do the following:

def ExecuteSQL(sql):
  con = DB.getConnection()
  with con:
    con.autocommit = True
    cur = con.cursor()
    cur.execute(sql)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM