.shutdown isn't so friendly :(

This commit is contained in:
Alex "mcmonkey" Goodwin 2024-09-19 17:24:51 +09:00
parent 5d8a0b7afd
commit 163bfff829

View File

@ -1,7 +1,6 @@
from __future__ import annotations from __future__ import annotations
import asyncio, threading import threading
from functools import partial, wraps
import os import os
import time import time
import mimetypes import mimetypes
@ -199,23 +198,31 @@ def recursive_search(directory: str, excluded_dir_names: list[str] | None=None)
logging.debug("recursive file list on directory {}".format(directory)) logging.debug("recursive file list on directory {}".format(directory))
with ThreadPoolExecutor() as executor: with ThreadPoolExecutor() as executor:
calls = []
def proc_subdir(path: str): def proc_subdir(path: str):
dirs[path] = os.path.getmtime(path) dirs[path] = os.path.getmtime(path)
def handle(file): def handle(file):
if not os.path.isdir(file): try:
relative_path = os.path.relpath(file, directory) if not os.path.isdir(file):
result.append(relative_path) relative_path = os.path.relpath(file, directory)
return result.append(relative_path)
executor.submit(lambda: proc_subdir(file)) return
for subdir in os.listdir(file):
path = os.path.join(file, subdir) calls.append(executor.submit(lambda: proc_subdir(file)))
if subdir not in excluded_dir_names:
executor.submit(lambda: handle(path)) for subdir in os.listdir(file):
path = os.path.join(file, subdir)
if subdir not in excluded_dir_names:
calls.append(executor.submit(lambda: handle(path)))
except Exception as e:
logging.error(f"Error while handling {file}: {e}")
calls.append(executor.submit(lambda: handle(directory)))
while len(calls) > 0:
calls.pop().result()
executor.submit(lambda: handle(directory))
executor.shutdown(wait=True)
logging.debug("found {} files".format(len(result))) logging.debug("found {} files".format(len(result)))
return result, dirs return result, dirs