|
2 | 2 | import time |
3 | 3 |
|
4 | 4 | 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. |
10 | 8 |
|
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 |
15 | 11 | """ |
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. |
17 | 15 |
|
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 |
20 | 20 |
|
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): |
25 | 22 | """ |
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. |
27 | 24 |
|
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 |
30 | 27 |
|
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 |
33 | 30 | """ |
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