MCPcopy
hub / github.com/huggingface/transformers / run_test_in_subprocess

Function run_test_in_subprocess

src/transformers/testing_utils.py:2780–2820  ·  view source on GitHub ↗

To run a test in a subprocess. In particular, this can avoid (GPU) memory issue. Args: test_case (`unittest.TestCase`): The test that will run `target_func`. target_func (`Callable`): The function implementing the actual testing logic. inputs

(test_case, target_func, inputs=None, timeout=None)

Source from the content-addressed store, hash-verified

2778
2779
2780def run_test_in_subprocess(test_case, target_func, inputs=None, timeout=None):
2781 """
2782 To run a test in a subprocess. In particular, this can avoid (GPU) memory issue.
2783
2784 Args:
2785 test_case (`unittest.TestCase`):
2786 The test that will run `target_func`.
2787 target_func (`Callable`):
2788 The function implementing the actual testing logic.
2789 inputs (`dict`, *optional*, defaults to `None`):
2790 The inputs that will be passed to `target_func` through an (input) queue.
2791 timeout (`int`, *optional*, defaults to `None`):
2792 The timeout (in seconds) that will be passed to the input and output queues. If not specified, the env.
2793 variable `PYTEST_TIMEOUT` will be checked. If still `None`, its value will be set to `600`.
2794 """
2795 if timeout is None:
2796 timeout = int(os.environ.get("PYTEST_TIMEOUT", "600"))
2797
2798 start_methohd = "spawn"
2799 ctx = multiprocessing.get_context(start_methohd)
2800
2801 input_queue = ctx.Queue(1)
2802 output_queue = ctx.JoinableQueue(1)
2803
2804 # We can't send `unittest.TestCase` to the child, otherwise we get issues regarding pickle.
2805 input_queue.put(inputs, timeout=timeout)
2806
2807 process = ctx.Process(target=target_func, args=(input_queue, output_queue, timeout))
2808 process.start()
2809 # Kill the child process if we can't get outputs from it in time: otherwise, the hanging subprocess prevents
2810 # the test to exit properly.
2811 try:
2812 results = output_queue.get(timeout=timeout)
2813 output_queue.task_done()
2814 except Exception as e:
2815 process.terminate()
2816 test_case.fail(e)
2817 process.join(timeout=timeout)
2818
2819 if results["error"] is not None:
2820 test_case.fail(f"{results['error']}")
2821
2822
2823def run_test_using_subprocess(func):

Callers 1

Calls 5

get_contextMethod · 0.80
joinMethod · 0.80
getMethod · 0.45
putMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected