Skip to content
Merged
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
53 changes: 53 additions & 0 deletions Week07/threaded_furkan_bulut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import threading
import time

def threaded(number_of_threads):
class Threaded:
"""
A class that acts as a decorator to run a function concurrently using multiple threads.

:param number_of_threads: The number of threads to spawn to run the decorated function.
:type number_of_threads: int
"""
def __init__(self, number_of_threads):
"""
Initializes the Threaded class with the specified number of threads.

:param number_of_threads: Number of threads to use for the decorated function.
:type number_of_threads: int
"""
self.number_of_threads = number_of_threads

def __call__(self, func):
"""
Makes the class instance callable and acts as a decorator for the provided function.

:param func: The function to be executed in multiple threads.
:type func: function

:return: The wrapper function that creates threads and runs the decorated function.
:rtype: function
"""
def wrapper(*args, **kwargs):
"""
Wrapper function that creates threads and runs the decorated function.

:param args: Positional arguments to pass to the decorated function.
:type args: tuple

:param kwargs: Keyword arguments to pass to the decorated function.
:type kwargs: dict
"""
threads = []

for i in range(self.number_of_threads):
thread = threading.Thread(target=func, args=args, kwargs=kwargs, name=f"Thread-{i}")
threads.append(thread)
thread.start()

for thread in threads:
thread.join()

return wrapper

return Threaded(number_of_threads)
Loading