From 777fb69be8c13b2c53456b8b7baa3196c3493a75 Mon Sep 17 00:00:00 2001 From: Furkan Baytak <91830994+FurkanBaytak@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:45:54 +0300 Subject: [PATCH] Add files via upload --- Week07/threaded_furkan_baytak.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Week07/threaded_furkan_baytak.py diff --git a/Week07/threaded_furkan_baytak.py b/Week07/threaded_furkan_baytak.py new file mode 100644 index 00000000..933f4ae9 --- /dev/null +++ b/Week07/threaded_furkan_baytak.py @@ -0,0 +1,32 @@ +import threading + +def threaded(n): + """ + Decorator to create n threads for a function. + + Args: + n (int): Number of threads to create. + + Returns: + function: A decorated function that runs in n threads. + """ + def decorator(func): + def wrapper(*args, **kwargs): + threads = [] + + # Function to be run in threads + def target_func(): + func(*args, **kwargs) + + # Create and start threads + for _ in range(n): + thread = threading.Thread(target=target_func) + threads.append(thread) + thread.start() + + # Wait for all threads to complete + for thread in threads: + thread.join() + + return wrapper + return decorator \ No newline at end of file