Python MySQL.connector.cursor中weakref的用途
通过使用MySQL官方python驱动程序MySQL.connector,以下代码段可以正常工作Python MySQL.connector.cursor中weakref的用途,python,mysql,Python,Mysql,通过使用MySQL官方python驱动程序MySQL.connector,以下代码段可以正常工作 # -*- coding: utf-8 -*- import mysql.connector conn = mysql.connector.connect(...) cursor = conn.cursor() cursor.execute(...) 但是当我使用链式调用创建游标时 # -*- coding: utf-8 -*- import mysql.connector cursor
# -*- coding: utf-8 -*-
import mysql.connector
conn = mysql.connector.connect(...)
cursor = conn.cursor()
cursor.execute(...)
但是当我使用链式调用创建游标时
# -*- coding: utf-8 -*-
import mysql.connector
cursor = mysql.connector.connect(...).cursor()
cursor.execute(...)
我遇到异常:ReferenceError:弱引用对象不再存在
这是由于在mysql.connector.cursor
源代码中使用了weakref
def _set_connection(self, connection):
"""Set the connection"""
try:
self._connection = weakref.proxy(connection)
self._connection._protocol # pylint: disable=W0212,W0104
except (AttributeError, TypeError):
raise errors.InterfaceError(errno=2048)
weakref不会增加对临时连接对象的引用计数,因此after语句
mysql.connector.connect(...).cursor()
连接对象似乎被垃圾收集回收
由于在mysql.connector.connection
源代码中,没有对游标对象的引用
mysql.connector.cursor
中的weakref可能未设置为解决循环引用问题
有人知道为什么要将weakref设置为对游标连接的引用吗
谢谢。如果您查看项目的修订历史记录,只需说:
- 现在大多数对象都使用弱引用:没有bug或泄漏,这表明我们需要这样做然而,情况也是如此。可以得到回响
因此,这似乎只是一个预防性的决定,以避免潜在的错误或泄漏,而不是解决特定问题的更改。您是否使用
bzr annotate
来查找提交?@unutbu我实际上使用了Launchpad源代码查看器中的“查看修订信息”选项,但我确信这只是在封面下使用了bzr annotate
。@dano我没有注意到launchpad有完整的提交历史记录。非常感谢。