mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-04-18 01:53:31 +00:00
Optimize file scanning performance using multi-threading
This commit is contained in:
parent
0e86405198
commit
82c3afe077
@ -5,6 +5,7 @@ import folder_paths
|
|||||||
import mimetypes
|
import mimetypes
|
||||||
import shutil
|
import shutil
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
@ -62,24 +63,29 @@ class OutputManager:
|
|||||||
def get_folder_items(self, folder: str):
|
def get_folder_items(self, folder: str):
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
with os.scandir(folder) as it:
|
def get_file_info(entry: os.DirEntry[str]):
|
||||||
for entry in it:
|
|
||||||
filepath = entry.path
|
filepath = entry.path
|
||||||
is_dir = entry.is_dir()
|
is_dir = entry.is_dir()
|
||||||
|
|
||||||
if not is_dir and not self.assert_file_type(filepath, ["image", "video", "audio"]):
|
if not is_dir and not self.assert_file_type(filepath, ["image", "video", "audio"]):
|
||||||
continue
|
return None
|
||||||
|
|
||||||
state = entry.stat()
|
stat = entry.stat()
|
||||||
result.append(
|
return {
|
||||||
{
|
|
||||||
"name": entry.name,
|
"name": entry.name,
|
||||||
"type": "folder" if entry.is_dir() else self.get_file_content_type(filepath),
|
"type": "folder" if entry.is_dir() else self.get_file_content_type(filepath),
|
||||||
"size": 0 if is_dir else state.st_size,
|
"size": 0 if is_dir else stat.st_size,
|
||||||
"createdAt": round(state.st_ctime_ns / 1000000),
|
"createdAt": round(stat.st_ctime_ns / 1000000),
|
||||||
"updatedAt": round(state.st_mtime_ns / 1000000),
|
"updatedAt": round(stat.st_mtime_ns / 1000000),
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
with os.scandir(folder) as it, ThreadPoolExecutor() as executor:
|
||||||
|
future_to_entry = {executor.submit(get_file_info, entry): entry for entry in it}
|
||||||
|
for future in as_completed(future_to_entry):
|
||||||
|
file_info = future.result()
|
||||||
|
if file_info is None:
|
||||||
|
continue
|
||||||
|
result.append(file_info)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user