Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing Sqlite3 in Python

Tags:

To fully utilize concurrency, SQLite3 allows threads to access the same connection in three ways:

  1. Single-thread. In this mode, all mutexes are disabled and SQLite is unsafe to use in more than a single thread at once.
  2. Multi-thread. In this mode, SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads.
  3. Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction.

Does anyone know how I can make the connection serialized in Python.
Python has "check_same_thread" which allows switching between multi-threading and single-threading; however, I can not find out how I should make it serialized.

like image 295
Masoud Valafar Avatar asked Jun 09 '11 16:06

Masoud Valafar


People also ask

How fetch data from sqlite3 database in Python?

SQLite Python: Querying Data First, establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.

What is sqlite3 in Python?

Source code: Lib/sqlite3/ SQLite is a C library that provides a lightweight disk-based database that doesn't require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage.


1 Answers

The Python SQLite module is not threadsafe. If you disable its checking then you need to ensure all code is serialized and that includes garbage collection. (My APSW module is threadsafe and also correctly handles the error message thread safety issues).

It is however safe to use multiple independent connections concurrently in the same process and I would recommend you do that. Additionally switch the database into write ahead logging mode and you should get very good performance even with lots of writing.

like image 143
Roger Binns Avatar answered Sep 24 '22 07:09

Roger Binns