Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A database for python 3?

I'm coding a small piece of server software for the personal use of several users. Not hundreds, not thousands, but perhaps 3-10 at a time.

Since it's a threaded server, SQLite doesn't work. It complains about threads like this:

ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 140735085562848 and this is thread id 4301299712

Besides, they say SQLite isn't great for concurrency anyhow.

Now since I started working with Python 3 (and would rather continue using it) I can't seem to get the MySQL module to work properly and others seem equally frustrated.

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

like image 622
cwj Avatar asked Dec 16 '25 15:12

cwj


1 Answers

First 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 you open a new handle to the database in each thread.

I do this using thread local storage to cache the database handle so there is only one per thread. Something like this... (from a py2.5 prog - hopefully 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
like image 51
Nick Craig-Wood Avatar answered Dec 19 '25 03:12

Nick Craig-Wood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!