From 70dda9dfc59de09a332605c2bbaf0e650af8eb13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkin=20Y=C4=B1ld=C4=B1r=C4=B1m?= <161759242+berkinyl@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:26:47 +0300 Subject: [PATCH 1/2] Create threaded_berkin_yildirim.py --- Week07/threaded_berkin_yildirim.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Week07/threaded_berkin_yildirim.py diff --git a/Week07/threaded_berkin_yildirim.py b/Week07/threaded_berkin_yildirim.py new file mode 100644 index 00000000..0d6b7555 --- /dev/null +++ b/Week07/threaded_berkin_yildirim.py @@ -0,0 +1,20 @@ +import threading + +def repeat_in_threads(times): + """ + Decorator to execute a function multiple times in separate threads. + """ + def apply_decorator(target_function): + def execute_in_threads(*args, **kwargs): + thread_list = [] + for _ in range(times): + t = threading.Thread(target=target_function, args=args, kwargs=kwargs) + thread_list.append(t) + t.start() + for t in thread_list: + t.join() + execute_in_threads.__name__ = target_function.__name__ + execute_in_threads.__doc__ = target_function.__doc__ + execute_in_threads.__module__ = target_function.__module__ + return execute_in_threads + return apply_decorator From 021db902fe9c07db354c892b801b24ee563041e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkin=20Y=C4=B1ld=C4=B1r=C4=B1m?= <161759242+berkinyl@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:36:14 +0300 Subject: [PATCH 2/2] Update threaded_berkin_yildirim.py --- Week07/threaded_berkin_yildirim.py | 33 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/Week07/threaded_berkin_yildirim.py b/Week07/threaded_berkin_yildirim.py index 0d6b7555..b47cd13b 100644 --- a/Week07/threaded_berkin_yildirim.py +++ b/Week07/threaded_berkin_yildirim.py @@ -1,20 +1,21 @@ import threading -def repeat_in_threads(times): +def threaded(n): """ - Decorator to execute a function multiple times in separate threads. + Decorator to run a function n times in separate threads. """ - def apply_decorator(target_function): - def execute_in_threads(*args, **kwargs): - thread_list = [] - for _ in range(times): - t = threading.Thread(target=target_function, args=args, kwargs=kwargs) - thread_list.append(t) - t.start() - for t in thread_list: - t.join() - execute_in_threads.__name__ = target_function.__name__ - execute_in_threads.__doc__ = target_function.__doc__ - execute_in_threads.__module__ = target_function.__module__ - return execute_in_threads - return apply_decorator + def decorator(func): + def wrapper(*args, **kwargs): + threads = [] + for i in range(n): + thread = threading.Thread(target=func, args=args, kwargs=kwargs) + threads.append(thread) + thread.start() + for thread in threads: + thread.join() + wrapper.__name__ = func.__name__ + wrapper.__doc__ = func.__doc__ + wrapper.__module__ = func.__module__ + return wrapper + return decorator +r