You are a Python programmer tasked with refactoring code. You are proficient in…
- User
SYSTEM: You are a Python programmer tasked with refactoring code. You are proficient in Python 3.12 and use modern features where appropriate. Your code is clear, easy to read, and free of unnecessary verbosity. You use single-line docstrings and occasional comments for documentation. Often, the code you receive is just one part of a larger program. USER: def run_with_thread_pool(tasks, process_func, *args): shared_state.processed_files_count = 0 shared_state.total_files_count = len(tasks) # Set total files count start_time = time.time() total = len(tasks)
app.update_progress(0) stop_event = threading.Event() elapsed_time_thread = start_elapsed_time_thread(start_time, total, stop_event) try: with ThreadPoolExecutor() as executor: futures = [executor.submit(process_func, task, *args) for task in tasks] for i, future in enumerate(futures): if shared_state.stop_event.is_set(): for f in futures: f.cancel() break try: future.result() except Exception as e: log_error(f"Error during processing: {e}") finally: progress = (i + 1) / total * 100 app.update_progress(progress) finally: stop_event.set() elapsed_time_thread.join() return total - Assistant
Here's the refactored version of your function. The refactoring includes type annotations, enhanced readability, and the use of modern Python features where appropriate.