2022-05-20 14:49:30 +01:00
|
|
|
"""Miscellaneous utilities."""
|
2024-06-10 17:07:47 +01:00
|
|
|
|
2023-01-11 20:09:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
import contextlib
|
|
|
|
import os
|
2023-01-11 20:09:37 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
|
|
|
|
from gymnasium.core import Env
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2022-12-04 22:24:02 +08:00
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
__all__ = ["CloudpickleWrapper", "clear_mpi_env_vars"]
|
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
|
2021-11-14 14:51:32 +01:00
|
|
|
class CloudpickleWrapper:
|
2022-05-20 14:49:30 +01:00
|
|
|
"""Wrapper that uses cloudpickle to pickle and unpickle the result."""
|
|
|
|
|
2023-01-11 20:09:37 +00:00
|
|
|
def __init__(self, fn: Callable[[], Env]):
|
2022-05-20 14:49:30 +01:00
|
|
|
"""Cloudpickle wrapper for a function."""
|
2019-06-21 17:29:44 -04:00
|
|
|
self.fn = fn
|
|
|
|
|
|
|
|
def __getstate__(self):
|
2022-05-20 14:49:30 +01:00
|
|
|
"""Get the state using `cloudpickle.dumps(self.fn)`."""
|
2019-06-21 17:29:44 -04:00
|
|
|
import cloudpickle
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
return cloudpickle.dumps(self.fn)
|
|
|
|
|
|
|
|
def __setstate__(self, ob):
|
2022-05-20 14:49:30 +01:00
|
|
|
"""Sets the state with obs."""
|
2019-06-21 17:29:44 -04:00
|
|
|
import pickle
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
self.fn = pickle.loads(ob)
|
|
|
|
|
|
|
|
def __call__(self):
|
2022-05-20 14:49:30 +01:00
|
|
|
"""Calls the function `self.fn` with no arguments."""
|
2019-06-21 17:29:44 -04:00
|
|
|
return self.fn()
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def clear_mpi_env_vars():
|
2022-05-20 14:49:30 +01:00
|
|
|
"""Clears the MPI of environment variables.
|
|
|
|
|
2023-11-07 13:27:25 +00:00
|
|
|
``from mpi4py import MPI`` will call ``MPI_Init`` by default.
|
2022-05-20 14:49:30 +01:00
|
|
|
If the child process has MPI environment variables, MPI will think that the child process
|
2019-06-21 17:29:44 -04:00
|
|
|
is an MPI process just like the parent and do bad things such as hang.
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
This context manager is a hacky way to clear those environment variables
|
|
|
|
temporarily such as when we are starting multiprocessing Processes.
|
2022-05-25 14:46:41 +01:00
|
|
|
|
|
|
|
Yields:
|
|
|
|
Yields for the context manager
|
2019-06-21 17:29:44 -04:00
|
|
|
"""
|
|
|
|
removed_environment = {}
|
|
|
|
for k, v in list(os.environ.items()):
|
2021-07-29 02:26:34 +02:00
|
|
|
for prefix in ["OMPI_", "PMI_"]:
|
2019-06-21 17:29:44 -04:00
|
|
|
if k.startswith(prefix):
|
|
|
|
removed_environment[k] = v
|
|
|
|
del os.environ[k]
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.environ.update(removed_environment)
|