question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLite :memory: is not working in threads

See original GitHub issue

The following doesn’t work:

import datetime
import threading
import time

import peewee

from playhouse import test_utils
from playhouse import sqlite_ext

db = sqlite_ext.SqliteExtDatabase('werd.sql')
test_db = sqlite_ext.SqliteExtDatabase(':memory:')


class SomeTable(peewee.Model):

    created_at = peewee.DateTimeField(default=datetime.datetime.now)

    class Meta:
        database = test_db


threads = []


def worker():
    print('db_instance', id(SomeTable._meta.database))  # same id here
    print('SomeTable count', SomeTable.select().count())  # will fail here
    return


peewee.create_model_tables([SomeTable], fail_silently=True)

SomeTable.create()
print('db_instance', id(SomeTable._meta.database), SomeTable.select().count())

for i in range(2):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()

    time.sleep(2)  # just so we don't exit before the worker is done

is SQLite in memory not meant for threading?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
coleifercommented, Feb 9, 2017

In-memory databases only exist for the connection that opened them. Peewee, by default, uses a connection-per-thread, so each thread has it’s own connection (and hence its own in-memory database). You can disable this by passing threadlocals=False to the db when initializing it. You might also need to pass check_same_thread=False, which is required to use pysqlite when you’re sharing a connection across threads.

0reactions
coleifercommented, Apr 3, 2018

You can use thread_safe=False. Sorry, I should have read the original issue description.

In 3.x, thread_safe=True means all threads have their own connection. You can pass thread_safe=False to make all your threads share the same connection to the underlying memory database.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sqlite3 In Memory Multithreaded Issues - Blocked threads ...
After running several tests, I've noticed the behavior differs substantially from non-in-memory databases and multithreading.
Read more >
SQLite Forum: Concurrency for in memory database
Hi all,. I have a particular use case where I would like to have multiple readers from an in memory database.
Read more >
sqlite3, memory db and multithreading
The problem is simple: I have multiple threads within one program. At least 2 threads have to have access to in-memory sqlite database....
Read more >
12118 (in-memory test database does not work with threads)
In the sqlite database backend, instead of using the database name :memory: , we should use a name such as file:testdb?mode=memory&cache=shared (where "testdb" ......
Read more >
cherrypy, sqlite & threading
My problem is how do i handle connections to sqlite db from my threads ... single connection to a sqlite :memory: db among...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found