diff --git a/datasette/app.py b/datasette/app.py index 6b0e6ada..c0071d2a 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -186,7 +186,8 @@ class Datasette: if db.name in self.databases: raise Exception("Multiple files with same stem: {}".format(db.name)) self.add_database(db.name, db) - self.scan_dirs() + self.scan_dirs_executor = futures.ThreadPoolExecutor(max_workers=1) + self.scan_dirs_executor.submit(self.scan_dirs) self.cache_headers = cache_headers self.cors = cors self._metadata = metadata or {} @@ -224,7 +225,6 @@ class Datasette: def scan_dirs(self): # Recurse through self.dirs looking for new SQLite DBs - i = 0 for dir in self.dirs: print(dir) for filepath in Path(dir).glob("**/*.db"): @@ -237,9 +237,6 @@ class Datasette: .replace(".db", ""), Database(self, filepath, is_mutable=True), ) - i += 1 - if i >= 20: - break def config(self, key): return self._config.get(key, None) diff --git a/datasette/views/index.py b/datasette/views/index.py index fe88a38c..91988d1c 100644 --- a/datasette/views/index.py +++ b/datasette/views/index.py @@ -23,7 +23,10 @@ class IndexView(BaseView): async def get(self, request, as_format): databases = [] - for name, db in self.ds.databases.items(): + # Using list() here because scan_dirs() running in a thread might + # modify self.ds.databases while we are iterating it, which could + # cause 'RuntimeError: OrderedDict mutated during iteration' + for name, db in list(self.ds.databases.items()): table_names = await db.table_names() hidden_table_names = set(await db.hidden_table_names()) views = await db.view_names()