Skip to content

Commit 021db90

Browse files
authored
Update threaded_berkin_yildirim.py
1 parent 70dda9d commit 021db90

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

Week07/threaded_berkin_yildirim.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import threading
22

3-
def repeat_in_threads(times):
3+
def threaded(n):
44
"""
5-
Decorator to execute a function multiple times in separate threads.
5+
Decorator to run a function n times in separate threads.
66
"""
7-
def apply_decorator(target_function):
8-
def execute_in_threads(*args, **kwargs):
9-
thread_list = []
10-
for _ in range(times):
11-
t = threading.Thread(target=target_function, args=args, kwargs=kwargs)
12-
thread_list.append(t)
13-
t.start()
14-
for t in thread_list:
15-
t.join()
16-
execute_in_threads.__name__ = target_function.__name__
17-
execute_in_threads.__doc__ = target_function.__doc__
18-
execute_in_threads.__module__ = target_function.__module__
19-
return execute_in_threads
20-
return apply_decorator
7+
def decorator(func):
8+
def wrapper(*args, **kwargs):
9+
threads = []
10+
for i in range(n):
11+
thread = threading.Thread(target=func, args=args, kwargs=kwargs)
12+
threads.append(thread)
13+
thread.start()
14+
for thread in threads:
15+
thread.join()
16+
wrapper.__name__ = func.__name__
17+
wrapper.__doc__ = func.__doc__
18+
wrapper.__module__ = func.__module__
19+
return wrapper
20+
return decorator
21+
r

0 commit comments

Comments
 (0)