1 from __future__ 
import absolute_import, division, print_function, unicode_literals
    10 from common_utils 
import (TestCase, run_tests, IS_WINDOWS, NO_MULTIPROCESSING_SPAWN)
    14 def test_success_func(i):
    18 def test_success_single_arg_func(i, arg):
    23 def test_exception_single_func(i, arg):
    25         raise ValueError(
"legitimate exception from process %d" % i)
    29 def test_exception_all_func(i):
    30     time.sleep(random.random() / 10)
    31     raise ValueError(
"legitimate exception from process %d" % i)
    34 def test_terminate_signal_func(i):
    36         os.kill(os.getpid(), signal.SIGABRT)
    40 def test_terminate_exit_func(i, arg):
    46 def test_success_first_then_exception_func(i, arg):
    50     raise ValueError(
"legitimate exception")
    53 def test_nested_child_body(i, ready_queue, nested_child_sleep):
    55     time.sleep(nested_child_sleep)
    58 def test_nested_spawn(i, pids_queue, nested_child_sleep):
    59     context = mp.get_context(
"spawn")
    60     nested_child_ready_queue = context.Queue()
    62     spawn_context = mp.spawn(
    63         fn=test_nested_child_body,
    64         args=(nested_child_ready_queue, nested_child_sleep),
    69     pids_queue.put(spawn_context.pids())
    73     for _ 
in range(nprocs):
    74         nested_child_ready_queue.get()
    77     os.kill(os.getpid(), signal.SIGTERM)
    81     NO_MULTIPROCESSING_SPAWN,
    82     "Disabled for environments that don't support the spawn start method")
    84     def test_success(self):
    85         mp.spawn(test_success_func, nprocs=2)
    87     def test_success_non_blocking(self):
    88         spawn_context = mp.spawn(test_success_func, nprocs=2, join=
False)
    91         spawn_context.join(timeout=
None)
    92         spawn_context.join(timeout=
None)
    93         self.assertTrue(spawn_context.join(timeout=
None))
    95     def test_first_argument_index(self):
    96         context = mp.get_context(
"spawn")
    97         queue = context.SimpleQueue()
    98         mp.spawn(test_success_single_arg_func, args=(queue,), nprocs=2)
    99         self.assertEqual([0, 1], sorted([queue.get(), queue.get()]))
   101     def test_exception_single(self):
   103         for i 
in range(nprocs):
   104             with self.assertRaisesRegex(
   106                 "\nValueError: legitimate exception from process %d$" % i,
   108                 mp.spawn(test_exception_single_func, args=(i,), nprocs=nprocs)
   110     def test_exception_all(self):
   111         with self.assertRaisesRegex(
   113             "\nValueError: legitimate exception from process (0|1)$",
   115             mp.spawn(test_exception_all_func, nprocs=2)
   117     def test_terminate_signal(self):
   119         message = 
"process 0 terminated with signal (SIGABRT|SIGIOT)"   127             message = 
"process 0 terminated with exit code 22"   129         with self.assertRaisesRegex(Exception, message):
   130             mp.spawn(test_terminate_signal_func, nprocs=2)
   132     def test_terminate_exit(self):
   134         with self.assertRaisesRegex(
   136             "process 0 terminated with exit code %d" % exitcode,
   138             mp.spawn(test_terminate_exit_func, args=(exitcode,), nprocs=2)
   140     def test_success_first_then_exception(self):
   142         with self.assertRaisesRegex(
   144             "ValueError: legitimate exception",
   146             mp.spawn(test_success_first_then_exception_func, args=(exitcode,), nprocs=2)
   149         sys.platform != 
"linux",
   150         "Only runs on Linux; requires prctl(2)",
   152     def test_nested_spawn(self):
   153         context = mp.get_context(
"spawn")
   154         pids_queue = context.Queue()
   155         nested_child_sleep = 20.0
   156         spawn_context = mp.spawn(
   157             fn=test_nested_spawn,
   158             args=(pids_queue, nested_child_sleep),
   165         pids = pids_queue.get()
   171                 except ProcessLookupError:
   179             self.assertLess(time.time() - start, nested_child_sleep / 2)
   183 if __name__ == 
'__main__':