I have a problem with executing long time queries using psycopg2 in Python. When query takes more than 180 seconds the script execution hangs up for a long time.
I use Python 3.4.3
and psycopg2 2.6.1
.
Here are samples to reproduce the issue:
import psycopg2
cnn = psycopg2.connect(
database='**********',
user='**********',
password='**********',
host='**********',
port=5432,
)
print("Connected")
cursor = cnn.cursor()
seconds = 5
print("Sleep %s seconds"%seconds)
cursor.execute("SELECT pg_sleep(%s);"%seconds)
print("Exit.")
Script works fine when query takes 5 seconds:
$python3 /tmp/test.py
Connected
Sleep 5 seconds
Exit.
But when number of seconds is about 180 and greater, the line cursor.execute
hangs up and instructions below are never executed:
import psycopg2
cnn = psycopg2.connect(
database='**********',
user='**********',
password='**********',
host='**********',
port=5432,
)
print("Connected")
cursor = cnn.cursor()
seconds = 180
print("Sleep %s seconds"%seconds)
cursor.execute("SELECT pg_sleep(%s);"%seconds)
print("Exit.")
Here is a output:
$python3 /tmp/test.py
Connected
Sleep 5 seconds
<Never exit>
Does anyone know how to solve this problem? Thank you.
You might have a statement timeout set somewhere. Try to turn it off for a single statement:
cursor = cnn.cursor()
seconds = 180
# Turn statement_timeout off for the next query
cursor.execute("SET statement_timeout = 0")
print("Sleep %s seconds"%seconds)
cursor.execute("SELECT pg_sleep(%s);"%seconds)
print("Exit.")
If this works, change the default, whereever you have defined it, or just for your connection:
cnn = psycopg2.connect(
database='**********',
user='**********',
password='**********',
host='**********',
port=5432,
options='-c statement_timeout=0'
)
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.