From 77b0abeea49bc45c6682614ea1e12b5b138b719e Mon Sep 17 00:00:00 2001 From: ErtugrulKGIT Date: Wed, 20 May 2026 14:16:30 +0300 Subject: [PATCH] Implement threaded execution decorator Added a decorator to execute functions in multiple threads, managing thread creation and synchronization. --- Week07/threaded_ertugrul_kose.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Week07/threaded_ertugrul_kose.py diff --git a/Week07/threaded_ertugrul_kose.py b/Week07/threaded_ertugrul_kose.py new file mode 100644 index 00000000..bb967ca2 --- /dev/null +++ b/Week07/threaded_ertugrul_kose.py @@ -0,0 +1,35 @@ +import threading +from functools import wraps + +def execute_in_threads(thread_total: int): + """ + A decorator that runs the decorated function in multiple threads. + It manages thread creation, execution, and synchronization. + """ + + def decorator(target_func): + + @wraps(target_func) + def wrapper(*func_args, **func_kwargs): + workers = [] + + # Create threads + for _ in range(thread_total): + thread = threading.Thread( + target=target_func, + args=func_args, + kwargs=func_kwargs + ) + workers.append(thread) + + # Start all threads + for thread in workers: + thread.start() + + # Wait for all threads to complete + for thread in workers: + thread.join() + + return wrapper + + return decorator