Comments (5)
Here's the change:
diff --git a/datasette/database.py b/datasette/database.py
index ffe94ea7..e56dcf03 100644
--- a/datasette/database.py
+++ b/datasette/database.py
@@ -85,12 +85,17 @@ class Database:
return "db"
def connect(self, write=False):
+ kwargs = {
+ "uri": True,
+ "check_same_thread": False,
+ }
+ if write:
+ kwargs["isolation_level"] = "IMMEDIATE"
if self.memory_name:
uri = "file:{}?mode=memory&cache=shared".format(self.memory_name)
conn = sqlite3.connect(
uri,
- uri=True,
- check_same_thread=False,
+ **kwargs,
)
if not write:
conn.execute("PRAGMA query_only=1")
@@ -110,9 +115,7 @@ class Database:
qs = ""
if self.mode is not None:
qs = f"?mode={self.mode}"
- conn = sqlite3.connect(
- f"file:{self.path}{qs}", uri=True, check_same_thread=False
- )
+ conn = sqlite3.connect(f"file:{self.path}{qs}", **kwargs)
self._all_file_connections.append(conn)
return conn
I don't want to commit this until I can run some kind of load test that demonstrates a problem with the default connection mode that is solved by switching to IMMEDIATE
.
from datasette.
I tried running this locustfile.py
using python -m locust -f locustfile.py
and then simulating 100 users.
I ran sqlite-utils enable-wal data.db
and then datasette data.db --root
and used the /-/create-token
page to create a token which I set as the DS_API_KEY
environment variable.
from locust import HttpUser, task, between
import random
import json
import os
class APIUser(HttpUser):
host = "http://localhost:8001"
# wait_time = between(0.05, 0.1) # in seconds
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.id_counter = 0
self.api_key = os.environ.get('DS_API_KEY')
if not self.api_key:
raise ValueError("DS_API_KEY environment variable is not set")
self.headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {self.api_key}'
}
@task(3)
def get_data(self):
self.client.get("/data", headers=self.headers)
@task(3)
def get_mytable(self):
self.client.get("/data/mytable?_sort_desc=id", headers=self.headers)
@task(2)
def get_mytable_with_id(self):
self.client.get("/data/mytable?id=5", headers=self.headers)
@task(4)
def post_create_data(self):
self.id_counter += 1
payload = {
"table": "mytable",
"rows": [
{
"id": self.id_counter,
"name": random.choice(["Tarantula", "Black Widow", "Huntsman", "Wolf Spider", "Jumping Spider"])
}
],
"pk": "id",
"replace": True
}
self.client.post("/data/-/create", data=json.dumps(payload), headers=self.headers)
On both my Mac and GitHub Codespaces I was unable to get it to trigger any database lock errors. Mac results:
I tried on Codespaces as well because I thought maybe my M2 performed too well and didn't trigger errors.
from datasette.
The currently open challenge here is this: create a locustfile.py
which reliably triggers database locked errors when exercising a Datasette instance calling the JSON write API (and reading other endpoints at the same time).
from datasette.
Here's Codespaces Lotust run with WAL enabled (the macOS one above didn't have WAL enabled):
I used https://github.com/datasette/studio to run this
from datasette.
I'm going to push this code anyway, and exercise it in an alpha for a bit.
from datasette.
Related Issues (20)
- Proposal - store metadata inside `internal.db` tables HOT 2
- Broken link in documention: fivethirtyeight.datasettes.com
- Fix font size on filter inputs
- base_url getting appended twice in redirects when applying filters? HOT 3
- Accessibility: add a `lang` attribute to `html` HOT 1
- What minimal SQLite version should Datasette support? HOT 9
- Remove upserts in `set_XXX_metadata()` methods
- PyOdide test failure HOT 7
- Canned queries with named parameters fail with error against SQLite 3.46.0 HOT 11
- derive_named_parameters() method that works with latest SQLite HOT 5
- Flaky test_max_csv_mb test HOT 3
- Very weird flaky test_create_table_ignore_replace and test_upsert tests HOT 18
- multiple plugins extending the same base template?
- Database/Table/Row not found errors echo back text from URL HOT 7
- Proposal — Datasette JSON API changes for 1.0 HOT 4
- Block or rate limit based on User Agent? HOT 3
- Datasette 1.0 Canned Queries Broken HOT 3
- [Error] no such table: main.events HOT 1
- Cannot run datasette on 3.13 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datasette.