[文档]defobs_n_space_info(obs_n_space):ifisinstance(obs_n_space,gym.spaces.Dict):assertisinstance(obs_n_space.spaces,OrderedDict)subspaces=obs_n_space.spaceselifisinstance(obs_n_space,gym.spaces.Tuple):subspaces={i:obs_n_space.spaces[i]foriinrange(len(obs_n_space_info.spaces))}elifisinstance(obs_n_space,dict):subspaces={k:obs_n_space[k]forkinobs_n_space.keys()}else:subspaces={None:obs_n_space}keys=[]shapes={}dtypes={}forkey,boxinsubspaces.items():keys.append(key)shapes[key]=box.shape# assume the obs_shapes are the same.dtypes[key]=box.dtypereturnkeys,shapes,dtypes
[文档]@contextlib.contextmanagerdefclear_mpi_env_vars():""" from mpi4py import MPI will call MPI_Init by default. If the child process has MPI environment variables, MPI will think that the child process is an MPI process just like the parent and do bad things such as hang. This context manager is a hacky way to clear those environment variables temporarily such as when we are starting multiprocessing Processes. """removed_environment={}fork,vinlist(os.environ.items()):forprefixin['OMPI_','PMI_']:ifk.startswith(prefix):removed_environment[k]=vdelos.environ[k]try:yieldfinally:os.environ.update(removed_environment)
[文档]classCloudpickleWrapper(object):""" Uses cloudpickle to serialize contents (otherwise multiprocessing tries to use pickle) """def__init__(self,x):self.x=xdef__getstate__(self):importcloudpicklereturncloudpickle.dumps(self.x)def__setstate__(self,ob):importpickleself.x=pickle.loads(ob)