MCPcopy Create free account
hub / github.com/pytorch/pytorch / ProcessContext

Class ProcessContext

torch/multiprocessing/spawn.py:87–188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

85
86
87class ProcessContext:
88 def __init__(self, processes, error_files):
89 self.error_files = error_files
90 self.processes = processes
91 self.sentinels = {
92 process.sentinel: index for index, process in enumerate(processes)
93 }
94
95 def pids(self):
96 return [int(process.pid) for process in self.processes]
97
98 def join(self, timeout=None):
99 r"""Join one or more processes within spawn context.
100
101 Attempt to join one or more processes in this spawn context.
102 If one of them exited with a non-zero exit status, this function
103 kills the remaining processes and raises an exception with the cause
104 of the first process exiting.
105
106 Returns ``True`` if all processes have been joined successfully,
107 ``False`` if there are more processes that need to be joined.
108
109 Args:
110 timeout (float): Wait this long before giving up on waiting.
111 """
112 # Ensure this function can be called even when we're done.
113 if len(self.sentinels) == 0:
114 return True
115
116 # Wait for any process to fail or all of them to succeed.
117 ready = multiprocessing.connection.wait(
118 self.sentinels.keys(),
119 timeout=timeout,
120 )
121
122 error_index = None
123 for sentinel in ready:
124 index = self.sentinels.pop(sentinel)
125 process = self.processes[index]
126 process.join()
127 if process.exitcode != 0:
128 error_index = index
129 break
130
131 # Return if there was no error.
132 if error_index is None:
133 # Return whether or not all processes have been joined.
134 return len(self.sentinels) == 0
135
136 # Assume failure. Terminate processes that are still alive.
137 # Try SIGTERM then SIGKILL if the process isn't going down.
138 # The reason is related to python signal handling is limited
139 # to main thread and if that is in c/c++ land and stuck it won't
140 # to handle it. We have seen processes getting stuck not handling
141 # SIGTERM for the above reason.
142 timeout: int = 30
143 for process in self.processes:
144 if process.is_alive():

Callers 1

start_processesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…