From f2fd7d20bf3d1ca7ea486e3e40f27576d44f39d0 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 15 Feb 2020 09:28:48 -0800 Subject: [PATCH] Run scan_dirs in a thread every 10 seconds --- datasette/app.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index c0071d2a..1b8be011 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -7,6 +7,7 @@ import os import re import sys import threading +import time import traceback import urllib.parse from concurrent import futures @@ -186,8 +187,11 @@ 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_executor = futures.ThreadPoolExecutor(max_workers=1) - self.scan_dirs_executor.submit(self.scan_dirs) + if dirs: + self.scan_dirs_thread = threading.Thread( + target=self.scan_dirs, name="scan-dirs", daemon=True + ) + self.scan_dirs_thread.start() self.cache_headers = cache_headers self.cors = cors self._metadata = metadata or {} @@ -225,18 +229,24 @@ class Datasette: def scan_dirs(self): # Recurse through self.dirs looking for new SQLite DBs - for dir in self.dirs: - print(dir) - for filepath in Path(dir).glob("**/*.db"): - print(filepath) - if is_valid_sqlite(filepath): - self.add_database( - str(filepath) - .replace("../", "") - .replace("/", "_") - .replace(".db", ""), - Database(self, filepath, is_mutable=True), - ) + while True: + current_filepaths = { + d.path for d in list(self.databases.values()) if d.path is not None + } + for dir in self.dirs: + for filepath in Path(dir).glob("**/*.db"): + if str(filepath) in current_filepaths: + continue + print(filepath) + if is_valid_sqlite(filepath): + self.add_database( + str(filepath) + .replace("../", "") + .replace("/", "_") + .replace(".db", ""), + Database(self, str(filepath), is_mutable=True), + ) + time.sleep(10) def config(self, key): return self._config.get(key, None)