File tree Expand file tree Collapse file tree 1 file changed +17
-16
lines changed Expand file tree Collapse file tree 1 file changed +17
-16
lines changed Original file line number Diff line number Diff line change 11import threading
22
3- def repeat_in_threads ( times ):
3+ def threaded ( n ):
44 """
5- Decorator to execute a function multiple times in separate threads.
5+ Decorator to run a function n times in separate threads.
66 """
7- def apply_decorator (target_function ):
8- def execute_in_threads (* args , ** kwargs ):
9- thread_list = []
10- for _ in range (times ):
11- t = threading .Thread (target = target_function , args = args , kwargs = kwargs )
12- thread_list .append (t )
13- t .start ()
14- for t in thread_list :
15- t .join ()
16- execute_in_threads .__name__ = target_function .__name__
17- execute_in_threads .__doc__ = target_function .__doc__
18- execute_in_threads .__module__ = target_function .__module__
19- return execute_in_threads
20- return apply_decorator
7+ def decorator (func ):
8+ def wrapper (* args , ** kwargs ):
9+ threads = []
10+ for i in range (n ):
11+ thread = threading .Thread (target = func , args = args , kwargs = kwargs )
12+ threads .append (thread )
13+ thread .start ()
14+ for thread in threads :
15+ thread .join ()
16+ wrapper .__name__ = func .__name__
17+ wrapper .__doc__ = func .__doc__
18+ wrapper .__module__ = func .__module__
19+ return wrapper
20+ return decorator
21+ r
You can’t perform that action at this time.
0 commit comments