From cc191661fa12a9b5b6cbe3988b48033e4dffafa1 Mon Sep 17 00:00:00 2001 From: Onur Konuk <95957528+onurkonukk@users.noreply.github.com> Date: Sun, 5 Jan 2025 09:58:30 +0300 Subject: [PATCH] Create threaded_onur_konuk.py --- Week07/threaded_onur_konuk.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Week07/threaded_onur_konuk.py diff --git a/Week07/threaded_onur_konuk.py b/Week07/threaded_onur_konuk.py new file mode 100644 index 00000000..7107634e --- /dev/null +++ b/Week07/threaded_onur_konuk.py @@ -0,0 +1,20 @@ +import threading + +def threaded(n): + """ + Decorator to run a function n times in separate threads. + + :param n: Number of threads to create and run the function in + :type n: int + """ + def decorator(func): + def wrapper(*args, **kwargs): + threads = [] + for i in range(n): + 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