简体   繁体   English

PostgreSQL + Python:关闭连接

[英]PostgreSQL + Python: Close connection

I made a game server in Python that connects to a PostgreSQL db using psycopg2.我用 Python 制作了一个游戏服务器,它使用 psycopg2 连接到 PostgreSQL 数据库。 I have seen examples, I have seen that when a connection to a data base is created, should close the connection when finished making queries, eg for each client:我看过例子,我已经看到当创建到数据库的连接时,应该在完成查询后关闭连接,例如对于每个客户端:

#create connection to db
con = psycopg2.connect (database = 'testdb', user = 'janbodnar')
cur = con.cursor ()
#process query
.
.
.
#close connection
con.close ()

Ok, when I start my server, I have this:好的,当我启动我的服务器时,我有这个:

Inside my class在我的课堂上

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

That is, I use the same connection object for all inquiries from all customers and never close the connection,this looks better than to be opening and closing connections for each client, my server apparently works well.也就是说,我对来自所有客户的所有查询使用相同的连接对象并且从不关闭连接,这看起来比为每个客户端打开和关闭连接更好,我的服务器显然运行良好。 you think of this?你想到了吗? this well done?这做得好吗? not to do?.不做?。 Thank you谢谢

@Michał Niklas Thanks for your answer and thanks for the correction self.con and self.cur, I forgot to put "self". @Michał Niklas 感谢您的回答,感谢您更正 self.con 和 self.cur,我忘了输入“self”。

I clarify that I know very little of servers and databases.我澄清一下,我对服务器和数据库知之甚少。

I intend to do this:我打算这样做:

My server handles "threads" separate processes for each user, then, in each separate process think open a connection for customer queries and then close this connection, something like this:我的服务器为每个用户处理“线程”单独的进程,然后,在每个单独的进程中,考虑为客户查询打开一个连接,然后关闭此连接,如下所示:

in my class: if a new request from client1... a "thread" for this client, then, the query runs...在我的班级中:如果来自 client1 的新请求...此客户端的“线程”,则查询运行...

def query (self):
       #create connection to db for client1
       con = psycopg2.connect (database = 'testdb', user = 'janbodnar')
       cur = con.cursor ()
       #process query for client1, for example ...
       cur.execute ("DROP TABLE IF EXISTS Cars")
       #close connection for this client
       con.close ()

what they think about this?他们对此有何看法? seems to me better.在我看来更好。 I appreciate suggestions and support.我感谢建议和支持。

This may work but is not good.这可能有效,但不好。 Problems: how to set datetime format for session?问题:如何为会话设置日期时间格式? How to handle transactions?如何处理交易? Temporary tables?临时表? How to handle errors?如何处理错误? See also at: How can I pool connections using psycopg and gevent?另请参阅: 如何使用 psycopg 和 gevent 池连接?

For such things you can use connection pooling.对于此类事情,您可以使用连接池。 This way when you start with new client (new network connection) you get db connection from pool.这样,当您开始使用新客户端(新网络连接)时,您会从池中获得 db 连接。 After using it instead of closing connection you release it and it returns to pool.使用它而不是关闭连接后,您将其释放并返回到池中。 Now it may be used by other thread.现在它可以被其他线程使用。

If your connection is somehow broken it may be simple closed instead of returning to pool.如果您的连接以某种方式中断,它可能会被简单地关闭而不是返回到池中。 Every thread can use transaction and you can change session settings like datetime format.每个线程都可以使用事务,您可以更改会话设置,例如日期时间格式。

I see that there is http://initd.org/psycopg/docs/pool.html我看到有http://initd.org/psycopg/docs/pool.html

PS In your methods you should use self.con and self.cur . PS 在你的方法中,你应该使用self.conself.cur

I think the answer to this is quite easy: as long as the total number of clients connected at the same time does not exceed your max_connections setting of your postgres service you should be fine.我认为这个问题的答案很简单:只要同时连接的客户端总数不超过 postgres 服务的max_connections设置,你应该没问题。 Otherwise new connections cannot be accepted.否则不能接受新的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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