From 8004f8b881c0b52c0de9f1d9773d73f805fe4be2 Mon Sep 17 00:00:00 2001 From: RichieHakim Date: Tue, 2 Apr 2024 02:12:19 -0400 Subject: [PATCH] more debug --- bnpm/path_helpers.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/bnpm/path_helpers.py b/bnpm/path_helpers.py index ce51e6e..b07727f 100644 --- a/bnpm/path_helpers.py +++ b/bnpm/path_helpers.py @@ -372,28 +372,14 @@ def check_files_openable(dir_outer, depth=2, time_limit_per_file=1, verbose=Fals indicating whether the file can be opened. """ import os - from concurrent.futures import ThreadPoolExecutor, TimeoutError + from multiprocessing import Pool, TimeoutError from contextlib import contextmanager - - def check_file_openable(file_path): - """ - Check if a file can be opened. - """ - try: - with open(file_path, 'rb') as f: - ## Read a maximum of 1024 bytes. If file is smaller, read the whole file. - f.read(1024) - print(f"File {file_path} can be opened.") if verbose > 1 else None - return True - except Exception as e: - print(f"File {file_path} could not be opened: {e}") if verbose > 0 else None - return False def check_with_timeout(file_path, time_limit): - with ThreadPoolExecutor(max_workers=1) as executor: - future = executor.submit(check_file_openable, file_path) + with Pool(processes=1) as executor: + result = executor.apply_async(_check_file_openable, (file_path, verbose,)) try: - return future.result(timeout=time_limit) + return result.get(timeout=time_limit) except TimeoutError: print(f"File {file_path} took too long to open.") if verbose > 0 else None return False @@ -414,4 +400,16 @@ def walk_files(dir_outer, depth): files = walk_files(dir_outer, depth) file_openable = {file: check_with_timeout(file, time_limit=time_limit_per_file) for file in files} return file_openable - \ No newline at end of file +def _check_file_openable(file_path, verbose=False): + """ + Check if a file can be opened. + """ + try: + with open(file_path, 'rb') as f: + ## Read a maximum of 1024 bytes. If file is smaller, read the whole file. + f.read(1024) + print(f"File {file_path} can be opened.") if verbose > 1 else None + return True + except Exception as e: + print(f"File {file_path} could not be opened: {e}") if verbose > 0 else None + return False