From f2d59534517e8b1f0567445b9e5b7628b7cd6743 Mon Sep 17 00:00:00 2001 From: Berke Alpaslan <93981165+BerkeAlpaslan@users.noreply.github.com> Date: Tue, 3 Dec 2024 22:53:25 +0300 Subject: [PATCH 1/2] Create threaded_berke_alpaslan.py --- Week07/threaded_berke_alpaslan.py | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Week07/threaded_berke_alpaslan.py diff --git a/Week07/threaded_berke_alpaslan.py b/Week07/threaded_berke_alpaslan.py new file mode 100644 index 00000000..fc3ddb70 --- /dev/null +++ b/Week07/threaded_berke_alpaslan.py @@ -0,0 +1,37 @@ +import threading + +""" +threaded(n) +============ + +A decorator to run a function multiple times in parallel threads. + +This decorator creates `n` threads to execute the decorated function. +Each thread uses the same arguments and keyword arguments passed to the function. +The main thread waits for all created threads to finish before continuing. + +Parameters +---------- +n : int + The number of threads to create for executing the function. + +Notes +----- +- The function is executed `n` times, each in a separate thread. +- Thread synchronization is handled using the `join()` method. +""" + +def threaded(n): + def decorator(func): + def wrapper(*args, **kwargs): + threads = [] + for number in range(n): + threads.append(threading.Thread(target=func, args=args, kwargs=kwargs)) + + for thread in threads: + thread.start() + + for thread in threads: + thread.join() + return wrapper + return decorator From 2b5b7c30085caec8ba1d0de1ca5dd9472797a88d Mon Sep 17 00:00:00 2001 From: Berke Alpaslan <93981165+BerkeAlpaslan@users.noreply.github.com> Date: Tue, 31 Dec 2024 00:01:09 +0300 Subject: [PATCH 2/2] Update threaded_berke_alpaslan.py --- Week07/threaded_berke_alpaslan.py | 83 ++++++++++++++++++------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/Week07/threaded_berke_alpaslan.py b/Week07/threaded_berke_alpaslan.py index fc3ddb70..5f6af962 100644 --- a/Week07/threaded_berke_alpaslan.py +++ b/Week07/threaded_berke_alpaslan.py @@ -1,37 +1,52 @@ import threading -""" -threaded(n) -============ - -A decorator to run a function multiple times in parallel threads. - -This decorator creates `n` threads to execute the decorated function. -Each thread uses the same arguments and keyword arguments passed to the function. -The main thread waits for all created threads to finish before continuing. - -Parameters ----------- -n : int - The number of threads to create for executing the function. - -Notes ------ -- The function is executed `n` times, each in a separate thread. -- Thread synchronization is handled using the `join()` method. -""" - def threaded(n): - def decorator(func): - def wrapper(*args, **kwargs): - threads = [] - for number in range(n): - threads.append(threading.Thread(target=func, args=args, kwargs=kwargs)) - - for thread in threads: - thread.start() - - for thread in threads: - thread.join() - return wrapper - return decorator + """Creates a decorator that runs the decorated function in multiple threads. + + This decorator creates `n` threads to execute the decorated function concurrently. + Each thread runs the same function with the same arguments. All threads are started + and then joined before returning. + + Parameters + ---------- + n : int + Number of threads to create and run the function in + + Returns + ------- + Threaded + A decorator class that handles the thread creation and management + + Examples + -------- + >>> @threaded(3) + ... def example_function(x): + ... print(f"Processing {x}") + ... + >>> example_function("test") + Processing test + Processing test + Processing test + + Notes + ----- + - All threads execute the same function with identical arguments + - The decorator waits for all threads to complete before returning + - No return values are captured from the threaded function executions + + """ + class Threaded: + def __init__(self, n): + self.n = n + def __call__(self, func): + def wrapper(*args, **kwargs): + threads = [] + for i in range(self.n): + t = threading.Thread(target=func, args=args, kwargs=kwargs) + threads.append(t) + for t in threads: + t.start() + for t in threads: + t.join() + return wrapper + return Threaded(n)