Database for python 3?

I code a small portion of the server software for personal use by several users. Not hundreds, not thousands, but maybe 3-10 at a time.

Since this is a streaming server, SQLite is not working. He complains about topics such as:

ProgrammingError: SQLite objects created in a stream can only be used in the same stream. The object was created in the stream identifier 140735085562848, and this is the stream identifier 4301299712

They also say that SQLite is not so good for concurrency.

Now that I have started working with Python 3 (and would prefer to continue using it), I cannot get MySQL to work correctly, while others seem to be disappointed.

In this case, is there any other DB option for Python 3 that I could consider?

+3
source share
6 answers

First of all, note that sqlite is thread safe.

$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite
>>> sqlite.threadsafety
1

Then just make sure that you open a new database descriptor in each thread.

I do this using a local thread store to cache the database descriptor, so there is only one for each thread. Something like this ... (from the py2.5 program - I hope it will work with 3.0!)

import threading

class YourClass:
    def __init__(self):
        #...
        self.local = threading.local()  # Thread local storage for db handles
        self.db_file = "/path/to/db"
        #...
    def db_open(self):
        if not getattr(self.local, "db", None):
            self.local.db = sqlite3.connect(self.db_file)
        return self.local.db
+7
source

I made psycopg2 port for Python 3.

+1
source

, Python Firebird, , Python 3. , Firebird-Python

+1

pymongo Python 3.

+1

You can create a new sqlite object in each thread, each of which uses the same database file. For such a small number of users, you may run into problems with concurrency, unless they all write very strongly to it.

0
source

Of course, the pragmatic option is to simply use one SQLite join for each thread.

0
source

Source: https://habr.com/ru/post/1719774/


All Articles