Skip to content

Commit 49a5b40

Browse files
authored
Merge pull request #819 from onurkonukk/patch-9
Create threaded_onur_konuk.py
2 parents 7fba28a + cc19166 commit 49a5b40

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Diff for: 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)