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

Pythonsqlite上运行我的单元测试时,我得到了这个熟悉的视线:

   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

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能