Is there a better way to return values from spawned functions?

If you want to pass the result from spawned processes back to the parent process, you can let the parent process create multiprocessing queues 31, pass it to children processes, and let children processes send result back through the queue. See the following code:

  
      github.com
  
  
    pytorch/pytorch/blob/cb26661fe4faf26386703180a9045e6ac6d157df/test/test_multiprocessing.py#L577-L600 45

@unittest.skipIf(NO_MULTIPROCESSING_SPAWN, "Disabled for environments that \
                 don't support multiprocessing with spawn start method")
@unittest.skipIf(not TEST_CUDA_IPC, 'CUDA IPC not available')
def test_event_multiprocess(self):
    event = torch.cuda.Event(enable_timing=False, interprocess=True)
    self.assertTrue(event.query())

    ctx = mp.get_context('spawn')
    p2c = ctx.SimpleQueue()
    c2p = ctx.SimpleQueue()
    p = ctx.Process(
        target=TestMultiprocessing._test_event_multiprocess_child,
        args=(event, p2c, c2p))
    p.start()

    c2p.get()  # wait for until child process is ready
    torch.cuda._sleep(50000000)  # spin for about 50 ms
    event.record()
    p2c.put(0)  # notify child event is recorded



  This file has been truncated. show original

  
  
    
    
  
  


If the result does not have to go back to the parent process, you can use gather 7 or allgather 1 to communicate the result across children processes.