@@ -2044,14 +2044,16 @@ def use_numexpr(use, min_elements=expr._MIN_ELEMENTS):
20442044 if inspect .isfunction (obj ) and name .startswith ('assert' ):
20452045 setattr (TestCase , name , staticmethod (obj ))
20462046
2047- def test_parallel (num_threads = 2 ):
2047+
2048+ def test_parallel (num_threads = 2 , kwargs_list = None ):
20482049 """Decorator to run the same function multiple times in parallel.
20492050
20502051 Parameters
20512052 ----------
20522053 num_threads : int, optional
20532054 The number of times the function is run in parallel.
2054-
2055+ kwargs_list : list of dicts, optional
2056+ The list of kwargs to update original function kwargs on different threads.
20552057 Notes
20562058 -----
20572059 This decorator does not pass the return value of the decorated function.
@@ -2061,14 +2063,23 @@ def test_parallel(num_threads=2):
20612063 """
20622064
20632065 assert num_threads > 0
2066+ has_kwargs_list = kwargs_list is not None
2067+ if has_kwargs_list :
2068+ assert len (kwargs_list ) == num_threads
20642069 import threading
20652070
20662071 def wrapper (func ):
20672072 @wraps (func )
20682073 def inner (* args , ** kwargs ):
2074+ if has_kwargs_list :
2075+ update_kwargs = lambda i : dict (kwargs , ** kwargs_list [i ])
2076+ else :
2077+ update_kwargs = lambda i : kwargs
20692078 threads = []
20702079 for i in range (num_threads ):
2071- thread = threading .Thread (target = func , args = args , kwargs = kwargs )
2080+ updated_kwargs = update_kwargs (i )
2081+ thread = threading .Thread (target = func , args = args ,
2082+ kwargs = updated_kwargs )
20722083 threads .append (thread )
20732084 for thread in threads :
20742085 thread .start ()
0 commit comments