SQLite :memory: is not working in threads
See original GitHub issueThe 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:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 passcheck_same_thread=False
, which is required to use pysqlite when you’re sharing a connection across threads.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 passthread_safe=False
to make all your threads share the same connection to the underlying memory database.