Skip to content

Commit c16f57e

Browse files
authored
Class based threaded_furkan_bulut.py
1 parent 8ce176a commit c16f57e

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

Week07/threaded_furkan_bulut.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,52 @@
22
import time
33

44
def threaded(number_of_threads):
5-
"""
6-
A decorator that runs a function concurrently using multiple threads.
7-
8-
:param number_of_threads: The number of threads to spawn to run the decorated function.
9-
:type number_of_threads: int
5+
class Threaded:
6+
"""
7+
A class that acts as a decorator to run a function concurrently using multiple threads.
108
11-
:return: A decorator to wrap a function with threaded execution.
12-
:rtype: function
13-
"""
14-
def decorator(func):
9+
:param number_of_threads: The number of threads to spawn to run the decorated function.
10+
:type number_of_threads: int
1511
"""
16-
The actual decorator that spawns the threads and runs the function.
12+
def __init__(self, number_of_threads):
13+
"""
14+
Initializes the Threaded class with the specified number of threads.
1715
18-
:param func: The function to be executed in multiple threads.
19-
:type func: function
16+
:param number_of_threads: Number of threads to use for the decorated function.
17+
:type number_of_threads: int
18+
"""
19+
self.number_of_threads = number_of_threads
2020

21-
:return: The wrapper function that creates threads and runs the decorated function.
22-
:rtype: function
23-
"""
24-
def wrapper(*args, **kwargs):
21+
def __call__(self, func):
2522
"""
26-
Wrapper function that creates threads and runs the decorated function.
23+
Makes the class instance callable and acts as a decorator for the provided function.
2724
28-
:param args: Positional arguments to pass to the decorated function.
29-
:type args: tuple
25+
:param func: The function to be executed in multiple threads.
26+
:type func: function
3027
31-
:param kwargs: Keyword arguments to pass to the decorated function.
32-
:type kwargs: dict
28+
:return: The wrapper function that creates threads and runs the decorated function.
29+
:rtype: function
3330
"""
34-
threads = []
35-
for i in range(number_of_threads):
36-
thread = threading.Thread(target=func, args=args, kwargs=kwargs, name=f"Thread-{i}")
37-
threads.append(thread)
38-
thread.start()
39-
for thread in threads:
40-
thread.join()
41-
return wrapper
42-
return decorator
31+
def wrapper(*args, **kwargs):
32+
"""
33+
Wrapper function that creates threads and runs the decorated function.
34+
35+
:param args: Positional arguments to pass to the decorated function.
36+
:type args: tuple
37+
38+
:param kwargs: Keyword arguments to pass to the decorated function.
39+
:type kwargs: dict
40+
"""
41+
threads = []
42+
43+
for i in range(self.number_of_threads):
44+
thread = threading.Thread(target=func, args=args, kwargs=kwargs, name=f"Thread-{i}")
45+
threads.append(thread)
46+
thread.start()
47+
48+
for thread in threads:
49+
thread.join()
50+
51+
return wrapper
52+
53+
return Threaded(number_of_threads)

0 commit comments

Comments
 (0)