Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 捕获导致SQLite多线程访问错误的对象_Python_Sqlite_Pdb - Fatal编程技术网

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

Python 捕获导致SQLite多线程访问错误的对象,python,sqlite,pdb,Python,Sqlite,Pdb,在Python和SQLite上运行单元测试时,我看到了一个熟悉的场景: SQLite objects created in a thread can only be used in that same thread. 测试运行正常,但仍在打印错误。我假设SQLite对象以某种方式泄漏到后台线程 有没有一种简单的方法来代替逐段剖析我的代码 将pdb断点放在某个地方,查看哪个线程中哪个对象导致了冲突(从而立即找出它最初是如何在那里结束的) 打印出带有错误消息的对象(以及引用它们的父对象)

在Python和SQLite上运行单元测试时,我看到了一个熟悉的场景:

   SQLite objects created in a thread can only be used in that same thread.
测试运行正常,但仍在打印错误。我假设SQLite对象以某种方式泄漏到后台线程

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

  • pdb
    断点放在某个地方,查看哪个线程中哪个对象导致了冲突(从而立即找出它最初是如何在那里结束的)

  • 打印出带有错误消息的对象(以及引用它们的父对象)


到目前为止,通过查看pysqlite的源代码,我可以说不可能只共享连接或游标对象(请参见
pysqlite\u check\u thread
函数用法)

pysqlite\u check\u线程
函数引发
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
线程140390877370112从
sqllitethread.py:30 worker conn.execute(“.tables”)
在线程中创建的SQLite对象只能在同一线程中使用。该对象是在线程id 140390912665408中创建的,即线程id 140390877370112

非常感谢您的全面回答(很抱歉赏金已经过期)。我会尝试一下,看看结果如何——不是很简单,但没想到会是这样。