Skip to content

Commit 331afa6

Browse files
authored
Merge pull request #1328 from sas5188/patch-8
Create threaded_murad_maharramli.py
2 parents f0a56b5 + 6d4ea30 commit 331afa6

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
for t in threads:
25+
t.join()
26+
27+
return wrapper
28+
return decorator

0 commit comments

Comments
 (0)