我想使用线程来连接postgresql 但postgresql日志显示:
与开放交易的客户端连接上的意外EOF
这是我的代码:
conn = []
for i in range(10):
conn.append(psycopg2.connect("dbname=test user=higis password=dbrgdbrg host=10.1.1.215 port=5432"))
print conn
def test_query(a,b,c,d,name,i):
try:
#conn = psycopg2.connect("dbname=test user=higis password=dbrgdbrg host=10.1.1.215 port=5432")
cur = conn[i].cursor()
sql_query = "SELECT count(*) FROM " + str(name) + " WHERE ST_MAKEENVELOPE" + str(
(a, b, c, d, 4326)) + "&& wkb_geometry"
start = time.time()
cur.execute(sql_query)
#conn.commit()
end = time.time()
results_time = end - start
results = cur.fetchall()
cur.close()
conn[i].close()
#print results
#print results[0][0]
suit_f = open("/home/pj/Desktop/test/log/replicate_query_time.txt", 'a')
print >> suit_f, (
'range query table:,%s,(%s %s %s %s),%s,%s' % (name, a, b, c, d, results[0][0], results_time))
except Exception, e:
print e
a_all = [1,2,3,4,5,6,7,8,9,10]
b_all = [1,2,3,4,5,6,7,8,9,10]
c_all = [1,2,3,4,5,6,7,8,9,10]
d_all = [1,2,3,4,5,6,7,8,9,10]
threads = []
for i in range(10):
a = a_all[i]
b = b_all[i]
c = c_all[i]
d = d_all[i]
t = threading.Thread(target=test_query,args=(a,b,c,d,"replicate_table1",i))
threads.append(t)
if __name__ == '__main__':
i = 0
for t in threads:
print "Thread:" + str(i) + " t and the time = %s" %(ctime())
t.setDaemon(True)
t.start()
i = i+1
t.join()
#conn.commit()
答案 0 :(得分:1)
在某些时候,服务器收到了一个客户端连接,该连接提供了相应的凭据(因此建立了会话)。
此日志消息通知您客户端已断开会话“它刚刚走开!”没有干净地关闭。也许代码在它到达cur.close()
语句之前抛出异常。