0

I am trying to implement the shared memory database. The objective is to allow non-blocking reading access to the database for the IPC among threading.

The setting is following

def __init__(self):
        self.fname=f"file::memory:?cache=shared"
        self.db = sqlite3.connect(self.fname,uri=True)
        cur = self.db.cursor()
        cur.execute('pragma journal_mode=wal')
        cur.close()

Here is how to insert and query the database

def put(self,tbn,data):
    cur = self.db.cursor()
    sql=f"""INSERT into {tbn} VALUES ({time.time()},{data});"""
    cur.execute(sql)
    self.db.commit()
    cur.close()
def get(self,tbn,timestamp=0):
    cur = self.db.cursor()
    sql=f"""Select data from {tbn} where timestamp>{timestamp}"""
    data=cur.execute(sql).fetchall()
    cur.close()
    return [d[0] for d in data]

and here is the worker to read and the main to write

def worker():
    buffer=DBBuffer()
    readed=0
    while 1:
        data=buffer.get(topic)
        time.sleep(0.0001)    
if __name__=="__main__":
    buffer=DBBuffer()
    buffer.init_table(topic)
    print("start")
    t = Thread(target=worker)
    t.daemon =True
    t.start()
    while 1:
        buffer.put(topic,time.time())
        
        time.sleep(1)
    
    del(buffer)





    

However, it gets the error:

    Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Program Files\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File ".\DBsharemenory.py", line 66, in worker
    data=buffer.get(topic)
  File ".\DBsharemenory.py", line 49, in get
    data=cur.execute(sql).fetchall()
sqlite3.OperationalError: database table is locked: topic1

Is there any solution for it?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.