def spawn_worker(self, request, context): port = find_free_port() cmd = [ sys.executable, # Path to the current Python binary. str((pathlib.Path(__file__).parent / "worker.py").absolute().resolve()), "--port", str(port), ] worker = subprocess.Popen(cmd) if worker.poll() == None: self._workers[port] = worker return manager_pb2.Port(num=port) context.set_details("Error in spawning worker subprocess.") context.set_code(grpc.StatusCode.INTERNAL) return manager_pb2.Port()
def terminate(self): # If the last action future returned is incomplete, cancel it first. if (self._act_future is not None) and (not self._act_future.done()): self._act_future.cancel() # Stop the remote worker process response = self._manager_stub.stop_worker( manager_pb2.Port(num=self._worker_address[1])) # Close manager channel self._manager_channel.close()
def terminate(self): """Close the agent connection and invalidate this agent.""" # If the last action future returned is incomplete, cancel it first. if (self._act_future is not None) and (not self._act_future.done()): self._act_future.cancel() try: # Stop the remote worker process self._manager_stub.stop_worker( manager_pb2.Port(num=self._worker_address[1])) # Close manager channel self._manager_channel.close() except grpc.RpcError as e: if e.code() == grpc.StatusCode.UNAVAILABLE: # Do nothing as RPC server has been terminated. pass else: raise e