【发布时间】:2014-12-11 08:39:48
【问题描述】:
我用 Python 制作了一个游戏服务器,它使用 psycopg2 连接到 PostgreSQL 数据库。我看过一些例子,我已经看到当创建到数据库的连接时,应该在完成查询后关闭连接,例如对于每个客户端:
#create connection to db
con = psycopg2.connect (database = 'testdb', user = 'janbodnar')
cur = con.cursor ()
#process query
.
.
.
#close connection
con.close ()
好的,当我启动我的服务器时,我有这个:
在我的课堂上
def __init __ (self):
#create connection to db
con = psycopg2.connect (database = 'testdb', user = 'janbodnar')
cur = con.cursor ()
# to all customers ...
def query(self):
#process query, for example ...
cur.execute ("DROP TABLE IF EXISTS Cars")
#the connection never closes
也就是说,我对所有客户的所有查询都使用相同的连接对象,并且从不关闭连接,这看起来比为每个客户端打开和关闭连接要好,我的服务器显然运行良好。你觉得这个? 这做得好吗?不做吗? 谢谢
【问题讨论】:
标签: python