P
P
Python Newbie2021-10-27 17:25:13
SQLite
Python Newbie, 2021-10-27 17:25:13

How to use sqlite python in multithreading?

I am making multiple threads using threaded . How do I use the built in sqlite3 module for multithreading?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antares4045, 2021-10-27
@Levman5

The connection must be raised ONCE in the main thread, and you can already make cursors from it on all threads (like). If it doesn't help: then just open-close the connection for each operation, for example: https://qna.habr.com/q/1062578#answer_2040432 .
But as in that answer, I will emphasize separately that it is better to raise a separate server of a normal database anyway and work with it in a normal multi-threaded mode. This is a pretty niche trick (this is sort of how browsers store their history).
Let's write down what is happening in general , so that when questions of related topics come across to my eyes, just throw a link here.
In absolutely any file system, multi-threaded access to a single file does not exist. at all. the program can read the file (or part of it) into RAM, and then replace the file with something calculated on the basis of the read (there is still an opportunity to do an "append", but this is syntactic sugar implemented at the OS level (maybe I'm wrong: experts correct me)).
Accordingly, if several programs simultaneously try to change one file, then in the end there will be just the result of the work of the last one sent for saving, and it will be built based on the version of the file that was at the time when this program read the file.
Sqlite is just a file on your disk, and if it weren’t for the protection that prevents you from creating a multi-threaded program, then it would seem to you that it doesn’t work at all: each thread sees in the database only what it did in the database, and after restarting, you generally see, at best, the version of the database from the thread that closed the connection last (or even a broken file).
In order to prevent all kinds of professionals from writing that “it’s not possible to use your sqlite”, protection was built in (a much more adequate analogue of which is on all microsoft office files) if at least someone is working with the file now, then you can’t open it and , unless otherwise noted, fails with an error.
The problem is as old as people trying to simultaneously change something in one subject. Unfortunately, this case, when a rather costly approach that comes to mind right away, is the only one that works in the general case (in your particular case, you can come up with some simpler rule, but the following will work always and everywhere): the first agent takes the standard, and leaves a note "I'm working, wait", makes his own changes and does not leave the standard with him, during his work, an already dissatisfied queue has already gathered at the note, which also needs to work - when the standard is released, the first in line takes it to himself, leaving his own note. etc.
The rules for organizing these queues are a whole science from which you can go crazy, but in the case of sqlite, we have exactly one configurable parameter: how long we are willing to wait.
According to the code:
no connections and cursors are created either globally or in a thread: we have a hot potato game - everyone is trying to keep the base for a minimum time.
Instead, every time you want to do something with the base instead

cursor.execute(clause, props)
result = cursor.fetchall()

or whatever you wrote
with sqlite3.connect(`Ваши параметры подключения`, timeout=`Какое-то зверски большое число секунд, которое мы готовы "стоять в очереди"`) as connect: 
  # создаём подключение к базе которое существует только в рамках блока with 
  # то что мы здесь напишем должно отработать максимально быстро
  cursor = connect.cursor()
  cursor.execute(clause, props)
  result = cursor.fetchall()

Ideally, put it in a separate function
p.s. if you came here in search of truth, then we continued to communicate here and eventually won.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question