我是否应该关闭与RejectDB中的数据库的每个连接?

2024-05-18 04:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用python构建的服务器,使用Tornado和rejectdb。它已经运行了很长一段时间,为每个用户打开到数据库的几个连接。我从来没有遇到过什么大问题,让它们开着,从不关上。但在查看日志文件时,我发现有很多这样的警告:

File "/home/bundleroot/commentserver/app.py", line 30, in <module>
        db_connection = r.connect(RDB_HOST,RDB_PORT) 
      File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 542, in connect
        return conn.reconnect(timeout)
      File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 475, in reconnect
        return self._instance.connect(timeout)
      File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 360, in connect
        self._socket = SocketWrapper(self, timeout)
      File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 268, in __init__
        (self.host, self.port, ex))
    rethinkdb.errors.RqlDriverError: Could not connect to localhost:28015. Error: [Errno 111] Connection refused
    Traceback (most recent call last):
      File "/home/bundleroot/commentserver/env34/lib/python3.4/site-packages/rethinkdb/net.py", line 244, in __init__
        socket.create_connection((self.host, self.port), timeout)
      File "/usr/lib/python3.4/socket.py", line 509, in create_connection
        raise err
      File "/usr/lib/python3.4/socket.py", line 500, in create_connection
        sock.connect(sa)
    ConnectionRefusedError: [Errno 111] Connection refused

我应该担心吗?在这种情况下,有什么好的做法?在


Tags: inpyselfhomenetlibpackagesconnect
1条回答
网友
1楼 · 发布于 2024-05-18 04:28:25

是的,您需要关闭连接以避免连接泄漏。这个烧瓶的例子来自ReinstDB,它可以:

https://github.com/rethinkdb/rethinkdb-example-flask-backbone-todo/blob/master/todo.py#L47-L65

即使ReinstDB没有对连接数实施硬限制(我不确定是否如此),也可能会遇到操作系统的限制。在

相关问题 更多 >