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
35 changes: 35 additions & 0 deletions Week07/threaded_ertugrul_kose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import threading
from functools import wraps

def execute_in_threads(thread_total: int):
"""
A decorator that runs the decorated function in multiple threads.
It manages thread creation, execution, and synchronization.
"""

def decorator(target_func):

@wraps(target_func)
def wrapper(*func_args, **func_kwargs):
workers = []

# Create threads
for _ in range(thread_total):
thread = threading.Thread(
target=target_func,
args=func_args,
kwargs=func_kwargs
)
workers.append(thread)

# Start all threads
for thread in workers:
thread.start()

# Wait for all threads to complete
for thread in workers:
thread.join()

return wrapper

return decorator