python - 捕获导致 SQLite 多线程访问错误的对象

标签 python sqlite pdb

在 Python 和 SQLite 上运行单元测试时,我看到了这种熟悉的景象:

   SQLite objects created in a thread can only be used in that same thread.

测试运行良好,但错误仍在打印出来。我假设 SQLite 对象以某种方式泄漏到后台线程。

有没有一种简单的方法来代替逐段剖析我的代码

  • 在某处放置 pdb 断点,然后查看在哪个线程中哪个对象导致了违规(从而立即弄清楚它最初是如何在那里结束的)。

    <
  • 使用错误消息以某种方式打印出对象(以及引用它们的父对象)

最佳答案

到目前为止,通过查看 pysqlite 的源代码,我可以说无法仅共享连接或游标对象(参见 pysqlite_check_thread 函数用法)。

pysqlite_check_thread 函数引发带有该消息的 ProgrammingError 异常

SQLite objects created in a thread can only be used in that same thread.

源代码中的某些函数捕获该异常并将其打印出来。

为了在您的源代码中找到在其他线程中调用连接方法的地方,我建议在连接对象上编写调用包装器,如下所示:

# script name: sqllitethread.py
import inspect
import sqlite3
import threading
import thread
from sqlite3 import ProgrammingError


class CallWrapper(object):

    def __init__(self, obj):
        self.obj = obj
        self.main_thread_id = thread.get_ident()

    def __getattr__(self, name):
        if self.main_thread_id != thread.get_ident():
            print "Thread %s requested `%s` attribute from %s" % (thread.get_ident(), name, self.obj)
            for frame in inspect.getouterframes(inspect.currentframe())[1:]:
                if frame[1].endswith('threading.py'):
                    continue
                print "\t", "%s:%s" % (frame[1], frame[2]), frame[3], frame[4][0].strip()
        return getattr(self.obj, name)


conn = CallWrapper(sqlite3.connect('example.db'))
c = conn.cursor()

def worker():
    try:
        conn.execute('.tables')
    except ProgrammingError, e:
        print e

t = threading.Thread(target=worker)
t.start()
t.join()

输出示例:

Thread 140390877370112 requested `execute` attribute from <sqlite3.Connection object at 0x7faf4e659858>
    sqllitethread.py:30 worker conn.execute('.tables')
SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 140390912665408 and this is thread id 140390877370112

关于python - 捕获导致 SQLite 多线程访问错误的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27884199/

相关文章:

python - 在 Google App Engine 下从数据库动态加载 Python 应用程序代码

linux - 为什么 sqlite3 不能在 Amazon Elastic File System 上运行?

android - 没有这样的列 : pratik (code 1): while compiling: DELETE FROM Insert_table WHERE NAME=pratik

python - 如何在pdb中设置自定义提示?

python - : possible?调用set_trace()时Pdb和Python的核心模块加载

python - 使用 ipdb 而不是 pdb 和 py.test --pdb 选项

javascript - 用于 Web 抓取的基于 Python 或 JS 的 REST API

python - 如何在 TensorFlow 中评估 MNIST 测试数据期间从每个输出节点获取值?

python - 如何强制 pprint 每行打印一个列表/元组/字典元素?

python - 在 python 中从 sqlite 调用 1 个单元格中的数据