Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Week07/threaded_murad_maharramli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import threading
from functools import wraps

def threaded(n: int):
"""
A decorator that executes the wrapped function in 'n' separate threads.
It handles creating, starting, and synchronizing (joining) the threads.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
threads = []

# Create n threads, pointing them to the target function
for _ in range(n):
t = threading.Thread(target=func, args=args, kwargs=kwargs)
threads.append(t)

# Start all threads
for t in threads:
t.start()

# Synchronize the threads by waiting for all of them to finish
for t in threads:
t.join()

return wrapper
return decorator
Loading