From 7d3a206453142d3831743a703bc7672e294e383e Mon Sep 17 00:00:00 2001 From: Furkan Bulut <93736726+Furk4nBulut@users.noreply.github.com> Date: Tue, 3 Dec 2024 22:58:07 +0300 Subject: [PATCH 1/4] Create threaded_furkan_bulut.py --- Week07/threaded_furkan_bulut.py | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Week07/threaded_furkan_bulut.py diff --git a/Week07/threaded_furkan_bulut.py b/Week07/threaded_furkan_bulut.py new file mode 100644 index 00000000..14d6729b --- /dev/null +++ b/Week07/threaded_furkan_bulut.py @@ -0,0 +1,61 @@ +import threading +import time + +def threaded(number_of_threads): + """ + A decorator to run a function in multiple threads. + + :param number_of_threads: The number of threads to create for the function. + :type number_of_threads: int + :return: A wrapped function that runs in the specified number of threads. + :rtype: function + """ + + def decorator_of_thread(func): + """ + Inner decorator to wrap the target function. + + :param func: The function to be executed in multiple threads. + :type func: function + :return: The wrapped function with threading enabled. + :rtype: function + """ + + def _wrapper(*args, **kwargs): + """ + Wrapper function to create, start, and synchronize threads. + + :param args: Positional arguments to pass to the target function. + :param kwargs: Keyword arguments to pass to the target function. + :return: None + :rtype: None + """ + threads = [] + for i in range(number_of_threads): + threads.append( + threading.Thread( + target=func, + args=args, + kwargs=kwargs, + name=f"Thread - {i}", + ) + ) + for thread in threads: + thread.start() + for thread in threads: + thread.join() + + return _wrapper + + return decorator_of_thread + + +@threaded(16) +def func(task_name): + print(f"{threading.current_thread().name} is starting task: {task_name}") + time.sleep(2) + print(f"{threading.current_thread().name} has finished task: {task_name}") + + +if __name__ == "__main__": + func("Learning") From 5f47b339e5cc9cd6622def41c00b8d6b73eeeff0 Mon Sep 17 00:00:00 2001 From: Furkan Bulut <93736726+Furk4nBulut@users.noreply.github.com> Date: Wed, 25 Dec 2024 14:23:21 +0300 Subject: [PATCH 2/4] Update threaded_furkan_bulut.py --- Week07/threaded_furkan_bulut.py | 44 +++++++++++---------------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/Week07/threaded_furkan_bulut.py b/Week07/threaded_furkan_bulut.py index 14d6729b..a4971f7b 100644 --- a/Week07/threaded_furkan_bulut.py +++ b/Week07/threaded_furkan_bulut.py @@ -3,52 +3,37 @@ def threaded(number_of_threads): """ - A decorator to run a function in multiple threads. + A decorator that runs a function concurrently using multiple threads. - :param number_of_threads: The number of threads to create for the function. + :param number_of_threads: The number of threads to spawn to run the decorated function. :type number_of_threads: int - :return: A wrapped function that runs in the specified number of threads. + + :return: A decorator to wrap a function with threaded execution. :rtype: function """ - - def decorator_of_thread(func): + def decorator(func): """ - Inner decorator to wrap the target function. + The actual decorator that spawns the threads and runs the function. :param func: The function to be executed in multiple threads. :type func: function - :return: The wrapped function with threading enabled. - :rtype: function """ - - def _wrapper(*args, **kwargs): + def wrapper(task_name, *args, **kwargs): """ - Wrapper function to create, start, and synchronize threads. + Wrapper function that creates threads and runs the decorated function. - :param args: Positional arguments to pass to the target function. - :param kwargs: Keyword arguments to pass to the target function. - :return: None - :rtype: None + :param task_name: The task name passed to the decorated function. + :type task_name: str """ threads = [] for i in range(number_of_threads): - threads.append( - threading.Thread( - target=func, - args=args, - kwargs=kwargs, - name=f"Thread - {i}", - ) - ) - for thread in threads: + thread = threading.Thread(target=func, args=(task_name,) + args, kwargs=kwargs, name=f"Thread-{i}") + threads.append(thread) thread.start() for thread in threads: thread.join() - - return _wrapper - - return decorator_of_thread - + return wrapper + return decorator @threaded(16) def func(task_name): @@ -56,6 +41,5 @@ def func(task_name): time.sleep(2) print(f"{threading.current_thread().name} has finished task: {task_name}") - if __name__ == "__main__": func("Learning") From 8ce176a9ed7f05c6781836171953092c43ae8860 Mon Sep 17 00:00:00 2001 From: Furkan Bulut <93736726+Furk4nBulut@users.noreply.github.com> Date: Fri, 27 Dec 2024 00:05:45 +0300 Subject: [PATCH 3/4] Update threaded_furkan_bulut.py --- Week07/threaded_furkan_bulut.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Week07/threaded_furkan_bulut.py b/Week07/threaded_furkan_bulut.py index a4971f7b..42e6a51e 100644 --- a/Week07/threaded_furkan_bulut.py +++ b/Week07/threaded_furkan_bulut.py @@ -17,29 +17,26 @@ def decorator(func): :param func: The function to be executed in multiple threads. :type func: function + + :return: The wrapper function that creates threads and runs the decorated function. + :rtype: function """ - def wrapper(task_name, *args, **kwargs): + def wrapper(*args, **kwargs): """ Wrapper function that creates threads and runs the decorated function. - :param task_name: The task name passed to the decorated function. - :type task_name: str + :param args: Positional arguments to pass to the decorated function. + :type args: tuple + + :param kwargs: Keyword arguments to pass to the decorated function. + :type kwargs: dict """ threads = [] for i in range(number_of_threads): - thread = threading.Thread(target=func, args=(task_name,) + args, kwargs=kwargs, name=f"Thread-{i}") + thread = threading.Thread(target=func, args=args, kwargs=kwargs, name=f"Thread-{i}") threads.append(thread) thread.start() for thread in threads: thread.join() return wrapper return decorator - -@threaded(16) -def func(task_name): - print(f"{threading.current_thread().name} is starting task: {task_name}") - time.sleep(2) - print(f"{threading.current_thread().name} has finished task: {task_name}") - -if __name__ == "__main__": - func("Learning") From c16f57e30db68a8bc88f69cb689bfb5fb07b9687 Mon Sep 17 00:00:00 2001 From: Furkan Bulut <93736726+Furk4nBulut@users.noreply.github.com> Date: Sat, 28 Dec 2024 13:00:30 +0300 Subject: [PATCH 4/4] Class based threaded_furkan_bulut.py --- Week07/threaded_furkan_bulut.py | 71 +++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/Week07/threaded_furkan_bulut.py b/Week07/threaded_furkan_bulut.py index 42e6a51e..a61dbef7 100644 --- a/Week07/threaded_furkan_bulut.py +++ b/Week07/threaded_furkan_bulut.py @@ -2,41 +2,52 @@ import time def threaded(number_of_threads): - """ - A decorator that runs a function concurrently using multiple threads. - - :param number_of_threads: The number of threads to spawn to run the decorated function. - :type number_of_threads: int + class Threaded: + """ + A class that acts as a decorator to run a function concurrently using multiple threads. - :return: A decorator to wrap a function with threaded execution. - :rtype: function - """ - def decorator(func): + :param number_of_threads: The number of threads to spawn to run the decorated function. + :type number_of_threads: int """ - The actual decorator that spawns the threads and runs the function. + def __init__(self, number_of_threads): + """ + Initializes the Threaded class with the specified number of threads. - :param func: The function to be executed in multiple threads. - :type func: function + :param number_of_threads: Number of threads to use for the decorated function. + :type number_of_threads: int + """ + self.number_of_threads = number_of_threads - :return: The wrapper function that creates threads and runs the decorated function. - :rtype: function - """ - def wrapper(*args, **kwargs): + def __call__(self, func): """ - Wrapper function that creates threads and runs the decorated function. + Makes the class instance callable and acts as a decorator for the provided function. - :param args: Positional arguments to pass to the decorated function. - :type args: tuple + :param func: The function to be executed in multiple threads. + :type func: function - :param kwargs: Keyword arguments to pass to the decorated function. - :type kwargs: dict + :return: The wrapper function that creates threads and runs the decorated function. + :rtype: function """ - threads = [] - for i in range(number_of_threads): - thread = threading.Thread(target=func, args=args, kwargs=kwargs, name=f"Thread-{i}") - threads.append(thread) - thread.start() - for thread in threads: - thread.join() - return wrapper - return decorator + def wrapper(*args, **kwargs): + """ + Wrapper function that creates threads and runs the decorated function. + + :param args: Positional arguments to pass to the decorated function. + :type args: tuple + + :param kwargs: Keyword arguments to pass to the decorated function. + :type kwargs: dict + """ + threads = [] + + for i in range(self.number_of_threads): + thread = threading.Thread(target=func, args=args, kwargs=kwargs, name=f"Thread-{i}") + threads.append(thread) + thread.start() + + for thread in threads: + thread.join() + + return wrapper + + return Threaded(number_of_threads)