Skip to content

Commit cc19166

Browse files
authored
Create threaded_onur_konuk.py
1 parent 133e6cb commit cc19166

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Week07/threaded_onur_konuk.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import threading
2+
3+
def threaded(n):
4+
"""
5+
Decorator to run a function n times in separate threads.
6+
7+
:param n: Number of threads to create and run the function in
8+
:type n: int
9+
"""
10+
def decorator(func):
11+
def wrapper(*args, **kwargs):
12+
threads = []
13+
for i in range(n):
14+
thread = threading.Thread(target=func, args=args, kwargs=kwargs, name=f"Thread-{i}")
15+
threads.append(thread)
16+
thread.start()
17+
for thread in threads:
18+
thread.join()
19+
return wrapper
20+
return decorator

0 commit comments

Comments
 (0)