We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents f0a56b5 + 6d4ea30 commit 331afa6Copy full SHA for 331afa6
1 file changed
Week07/threaded_murad_maharramli.py
@@ -0,0 +1,28 @@
1
+import threading
2
+from functools import wraps
3
+
4
+def threaded(n: int):
5
+ """
6
+ A decorator that executes the wrapped function in 'n' separate threads.
7
+ It handles creating, starting, and synchronizing (joining) the threads.
8
9
+ def decorator(func):
10
+ @wraps(func)
11
+ def wrapper(*args, **kwargs):
12
+ threads = []
13
14
+ # Create n threads, pointing them to the target function
15
+ for _ in range(n):
16
+ t = threading.Thread(target=func, args=args, kwargs=kwargs)
17
+ threads.append(t)
18
19
+ # Start all threads
20
+ for t in threads:
21
+ t.start()
22
23
+ # Synchronize the threads by waiting for all of them to finish
24
25
+ t.join()
26
27
+ return wrapper
28
+ return decorator
0 commit comments