SQLITE3上下文管理器具有Python 2.7中的文件锁定



这是在python 2.7下的上下文管理器中重新输入文件锁定的好方法吗?我只想确保Rlock()将有效,因此我可以获取一个多线程应用程序来使用单个数据库文件。

import sqlite3
import threading
import os
class ConnectionHolder:
    def __init__(self, connection):
        self.path = connection
        self.lock = threading.RLock()
    def __enter__(self):
        self.lock.acquire()
        self.connection = sqlite3.connect(self.path)
        self.cursor = self.connection .cursor()
        return self.cursor

    def __exit__(self, exc_class, exc, traceback):
        self.connection .commit()
        self.connection .close()
        self.lock.release()
conn_holder = ConnectionHolder(os.path.join(os.path.dirname(__file__), 'data/db/database.db'))
if __name__ == '__main__':
    with conn_holder as c:
        c.execute("SELECT * FROM 'sample_table'")
        result = c.fetchall()
        print result

我终于在代码中喜欢一个在提交前经营长循环(约2分钟)的地方。我对此进行了纠正,正如@DB建议的那样,将忙碌的超时增加到30秒。问题似乎已经解决。谢谢大家!