浏览代码

close sqlite3 connections if unused

Nick Sweeting 4 年之前
父节点
当前提交
62089fdb22
共有 1 个文件被更改,包括 9 次插入1 次删除
  1. 9 1
      archivebox/config.py

+ 9 - 1
archivebox/config.py

@@ -1102,7 +1102,15 @@ def setup_django(out_dir: Path=None, check_db=False, config: ConfigDict=CONFIG,
                 cache.get('test', None)
                 cache.get('test', None)
             except django.db.utils.OperationalError:
             except django.db.utils.OperationalError:
                 call_command("createcachetable", verbosity=0)
                 call_command("createcachetable", verbosity=0)
-            
+
+
+            # if archivebox gets imported multiple times, we have to close
+            # the sqlite3 whenever we init from scratch to avoid multiple threads
+            # sharing the same connection by accident
+            from django.db import connections
+            for conn in connections.all():
+                conn.close_if_unusable_or_obsolete()
+
             sql_index_path = Path(output_dir) / SQL_INDEX_FILENAME
             sql_index_path = Path(output_dir) / SQL_INDEX_FILENAME
             assert sql_index_path.exists(), (
             assert sql_index_path.exists(), (
                 f'No database file {SQL_INDEX_FILENAME} found in: {config["OUTPUT_DIR"]} (Are you in an ArchiveBox collection directory?)')
                 f'No database file {SQL_INDEX_FILENAME} found in: {config["OUTPUT_DIR"]} (Are you in an ArchiveBox collection directory?)')