Try smth like this
from concurrent.futures import ThreadPoolExecutor
import threading
def extract_file_threaded(args):
full_file_path, output_path = args
thread_id = threading.current_thread().ident
print(f"[Thread {thread_id}] Extracting {full_file_path}")
try:
with tarfile.open(full_file_path, "r:gz") as tar:
tar.extractall(path=output_path)
return f"Success: {full_file_path}"
except Exception as e:
return f"Error: {full_file_path} - {e}"
Use ThreadPoolExecutor instead of multiprocessing
with ThreadPoolExecutor(max_workers=min(len(files_to_extract), num_cores * 2)) as executor:
results = list(executor.map(extract_file_threaded,
[(f, output_path) for f in files_to_extract]))