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

def threaded(n:int) ->callable:
"""
A decorator that runs a function using multiple threads.

:param n: Number of threads to create
:type n: int
:return: A decorator function for threading
:rtype: function
"""
# if n < 1 or not isinstance(n, int):
# raise ValueError("Not Valid call of the decorator")

def decorator(func:callable) ->callable:
"""
An inner decorator function that wraps the original function with threads.

:param func: The original function to be run in threads
:type func: function
:return: A wrapper function that runs with threads
:rtype: function
"""
if not hasattr(func, "__call__"):
raise ValueError("Not Valid Function")

def wrapper(*args, **kwargs)->None:
"""
A wrapper function that runs the function with multiple threads.

:param args: Positional arguments to pass to the function
:param kwargs: Keyword arguments to pass to the function
"""

all_threads = []
try:
for _ in range(n):

one_thread = threading.Thread(target=func, args=args, kwargs=kwargs)
all_threads.append(one_thread)
one_thread.start()

for thread in all_threads:
thread.join()

except threading.ThreadError as te:
print(te)

return wrapper

return decorator
Loading