通过使用正式的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 = mysql.connector.connect(...).cursor()
cursor.execute(...)我得到了例外:ReferenceError:弱引用对象不再存在
这是由于在源代码中使用了弱引用。
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)弱引用不会增加对临时连接对象的引用计数,因此在语句之后
mysql.connector.connect(...).cursor()连接对象似乎是由垃圾回收回收的。
因为在mysql.connector.connection源代码中,没有引用游标对象。
mysql.connector.cursor中的弱引用可能不是用来解决循环引用问题的。
有谁知道为什么要将弱引用设置为引用游标的连接?
谢谢。
发布于 2014-09-13 15:16:27
如果您检查该项目的修订历史记录,weakref只需这样说:
因此,这似乎只是一个预防决定,以避免潜在的漏洞或泄漏,而不是一个改变,以解决一个特定的问题。
https://stackoverflow.com/questions/25824072
复制相似问题