Run scan_dirs() in a thread

This commit is contained in:
Simon Willison 2020-02-13 21:58:48 -08:00
commit 55e633e09f
2 changed files with 6 additions and 6 deletions

View file

@ -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)

View file

@ -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()