Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14011

SQLite3 read issue with multiple simultaneous access to db

$
0
0

I am having some trouble with data reads with my SQLite3 database.

Context:I have built a python program that relies on two scripts running in parallel:

  • Script1: Reads on an ongoing basis (on a time.sleep(30)) messages streamed from an MQTT broker and writes some data in table1 of my SQLite database (let's call it database1).
  • Script2: Has a number of processes which rely on access to database1. Whilst most processes involve reading/writing on other tables of database1, one process that has sporadic issues is the one that relies on reading table1 of database1 whilst script1 is running. The issue being that the reading of table1 from script2 may be skipped altogether.

From a lot of googling/reading forum threads and SQLite documentation, I believe this is due to the fact that whilst script1 writes into table1 of database1, the table1 becomes blocked of access. This becomes a problem when the process of script2 coincides with the write of script1 on the same database1 table1. I note that when I only run script2 (and do not execute script1), I never have this issue (i.e. all reads of table1 from script2 are never skipped), and everything process run smoothly.

I have tried a number of things including adding a timeout to my db connection and a pragma busy timeout (snippet bellow). Both timeouts have been applied to all the db functions I used in the two scripts but the results are the same.

def db_function1(param1,param2,param3):    con=None    temp_df='null'    db=os.path.join(os.getcwd(), './database1.db')    try:        con=sqlite3.connect(db, timeout=20) #Increase timeout from default 5sec.        c = con.cursor()        c.execute('''pragma busy_timeout=10000''') #Busy timeout changed to 10s (in ms)        temp_df = pd.read_sql('''SELECT * FROM table1 WHERE .....''' #I skipped the query detail to make this block neater to read            % (param1,param2,param3), con)        con.close()    except Error as e:        print(e)    return temp_df

Further to reading https://www.sqlite.org/faq.html#q5, it seems a path forward would be to use sqlite3_busy_handler() or sqlite3_busy_timeout() to run a test if the table is in fact busy so that it can wait and re-try (by pausing the process of script1 that relies on table1 reads when busy for a few seconds ), but I can't manage to get it to work or find any application example online.

Any ideas?

Thanks!


Viewing all articles
Browse latest Browse all 14011

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>