Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MySQL.connector.cursor中weakref的用途_Python_Mysql - Fatal编程技术网

Python MySQL.connector.cursor中weakref的用途

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

通过使用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 = 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有完整的提交历史记录。非常感谢。