env_with_subprocess.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import atexit
  2. import gymnasium as gym
  3. from gymnasium.spaces import Discrete
  4. import os
  5. import subprocess
  6. class EnvWithSubprocess(gym.Env):
  7. """An env that spawns a subprocess."""
  8. # Dummy command to run as a subprocess with a unique name
  9. UNIQUE_CMD = "sleep 20"
  10. def __init__(self, config):
  11. self.UNIQUE_FILE_0 = config["tmp_file1"]
  12. self.UNIQUE_FILE_1 = config["tmp_file2"]
  13. self.UNIQUE_FILE_2 = config["tmp_file3"]
  14. self.UNIQUE_FILE_3 = config["tmp_file4"]
  15. self.action_space = Discrete(2)
  16. self.observation_space = Discrete(2)
  17. # Subprocess that should be cleaned up.
  18. self.subproc = subprocess.Popen(self.UNIQUE_CMD.split(" "), shell=False)
  19. self.config = config
  20. # Exit handler should be called.
  21. atexit.register(lambda: self.subproc.kill())
  22. if config.worker_index == 0:
  23. atexit.register(lambda: os.unlink(self.UNIQUE_FILE_0))
  24. else:
  25. atexit.register(lambda: os.unlink(self.UNIQUE_FILE_1))
  26. def close(self):
  27. if self.config.worker_index == 0:
  28. os.unlink(self.UNIQUE_FILE_2)
  29. else:
  30. os.unlink(self.UNIQUE_FILE_3)
  31. def reset(self, *, seed=None, options=None):
  32. return 0, {}
  33. def step(self, action):
  34. return 0, 0, True, False, {}