Python sqlite3 and concurrency

前端 未结 14 972
南笙
南笙 2020-11-30 18:00

I have a Python program that uses the \"threading\" module. Once every second, my program starts a new thread that fetches some data from the web, and stores this data to my

14条回答
  •  被撕碎了的回忆
    2020-11-30 18:34

    The most likely reason you get errors with locked databases is that you must issue

    conn.commit()
    

    after finishing a database operation. If you do not, your database will be write-locked and stay that way. The other threads that are waiting to write will time-out after a time (default is set to 5 seconds, see http://docs.python.org/2/library/sqlite3.html#sqlite3.connect for details on that).

    An example of a correct and concurrent insertion would be this:

    import threading, sqlite3
    class InsertionThread(threading.Thread):
    
        def __init__(self, number):
            super(InsertionThread, self).__init__()
            self.number = number
    
        def run(self):
            conn = sqlite3.connect('yourdb.db', timeout=5)
            conn.execute('CREATE TABLE IF NOT EXISTS threadcount (threadnum, count);')
            conn.commit()
    
            for i in range(1000):
                conn.execute("INSERT INTO threadcount VALUES (?, ?);", (self.number, i))
                conn.commit()
    
    # create as many of these as you wish
    # but be careful to set the timeout value appropriately: thread switching in
    # python takes some time
    for i in range(2):
        t = InsertionThread(i)
        t.start()
    

    If you like SQLite, or have other tools that work with SQLite databases, or want to replace CSV files with SQLite db files, or must do something rare like inter-platform IPC, then SQLite is a great tool and very fitting for the purpose. Don't let yourself be pressured into using a different solution if it doesn't feel right!

提交回复
热议问题