Python SQLite: database is locked

后端 未结 20 1174
一生所求
一生所求 2020-12-12 14:24

I\'m trying this code:

import sqlite

connection = sqlite.connect(\'cache.db\')
cur = connection.cursor()
cur.execute(\'\'\'create table item
  (id integer p         


        
20条回答
  •  眼角桃花
    2020-12-12 15:06

    in my case ,the error happened when a lot of concurrent process trying to read/write to the same table. I used retry to workaround the issue

    def _retry_if_exception(exception):
        return isinstance(exception, Exception)
    
    @retry(retry_on_exception=_retry_if_exception,
           wait_random_min=1000,
           wait_random_max=5000,
           stop_max_attempt_number=5)
    def execute(cmd, commit=True):
       c.execute(cmd)
       c.conn.commit()
    

提交回复
热议问题