nepyope commited on
Commit
4c22ccb
·
verified ·
1 Parent(s): a2aa903

Update env.py

Browse files
Files changed (1) hide show
  1. env.py +43 -18
env.py CHANGED
@@ -1,18 +1,33 @@
 
 
1
  from pathlib import Path
2
  import subprocess
3
  import sys
 
 
 
4
  import gymnasium as gym
5
  from gymnasium import spaces
6
  import numpy as np
7
  from huggingface_hub import snapshot_download
8
- import os
9
- import signal
10
- snapshot_download("lerobot/unitree-g1-mujoco")
11
 
12
 
13
- def make_env(n_envs=1, use_async_envs=False, **kwargs):
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # define run_sim FIRST
16
  repo_dir = Path(__file__).parent
17
  run_sim = repo_dir / "run_sim.py"
18
 
@@ -21,9 +36,13 @@ def make_env(n_envs=1, use_async_envs=False, **kwargs):
21
  print("path:", run_sim)
22
  print("=" * 60)
23
 
24
- # now you can launch it
25
- proc = subprocess.Popen([sys.executable, str(run_sim)])
26
- print(f"simulator started as pid={proc.pid}")
 
 
 
 
27
 
28
  class DummyEnv(gym.Env):
29
  metadata = {"render_modes": []}
@@ -41,30 +60,36 @@ def make_env(n_envs=1, use_async_envs=False, **kwargs):
41
  return np.zeros(1, dtype=np.float32), 0.0, False, False, {}
42
 
43
  def close(self):
 
44
  pass
45
 
46
-
47
  def kill_sim(self):
48
- """manually kill the simulator process group (parent + children)."""
49
  if self.process is None:
50
  return
51
-
52
  try:
53
  pgid = os.getpgid(self.process.pid)
54
  except ProcessLookupError:
55
- # process already gone
56
  return
57
-
58
- print(f"killing simulator process group pgid={pgid} (pid={self.process.pid})...")
 
 
 
59
  # send SIGTERM to the whole group
60
- os.killpg(pgid, signal.SIGTERM)
61
-
 
 
 
62
  try:
63
  self.process.wait(timeout=2)
64
  except subprocess.TimeoutExpired:
65
  print("force killing simulator process group...")
66
  os.killpg(pgid, signal.SIGKILL)
67
-
68
  self.process = None
69
 
70
- return DummyEnv(proc)
 
1
+ # env.py
2
+
3
  from pathlib import Path
4
  import subprocess
5
  import sys
6
+ import os
7
+ import signal
8
+
9
  import gymnasium as gym
10
  from gymnasium import spaces
11
  import numpy as np
12
  from huggingface_hub import snapshot_download
13
+
14
+ # ensure the repo snapshot exists (no-op if already cached)
15
+ snapshot_download(repo_id="lerobot/unitree-g1-mujoco", revision=None, cache_dir=None)
16
 
17
 
18
+ def make_env(
19
+ n_envs=1,
20
+ use_async_envs: bool = False,
21
+ publish_images=True,
22
+ camera_port=5555,
23
+ cameras=None,
24
+ **kwargs,
25
+ ):
26
+ """
27
+ launch run_sim.py as a subprocess (non-blocking),
28
+ then return a dummy gym env.
29
+ """
30
 
 
31
  repo_dir = Path(__file__).parent
32
  run_sim = repo_dir / "run_sim.py"
33
 
 
36
  print("path:", run_sim)
37
  print("=" * 60)
38
 
39
+ # start simulator in its own process group
40
+ proc = subprocess.Popen(
41
+ [sys.executable, str(run_sim)],
42
+ preexec_fn=os.setsid, # new session / new pgid == pid
43
+ )
44
+
45
+ print(f"simulator started as pid={proc.pid}, pgid={os.getpgid(proc.pid)}")
46
 
47
  class DummyEnv(gym.Env):
48
  metadata = {"render_modes": []}
 
60
  return np.zeros(1, dtype=np.float32), 0.0, False, False, {}
61
 
62
  def close(self):
63
+ # do nothing: lerobot will keep the sim across env lifetime
64
  pass
65
 
 
66
  def kill_sim(self):
67
+ """kill the run_sim process group (parent + its children, e.g. image publisher)"""
68
  if self.process is None:
69
  return
70
+
71
  try:
72
  pgid = os.getpgid(self.process.pid)
73
  except ProcessLookupError:
74
+ # already dead
75
  return
76
+
77
+ print(
78
+ f"killing simulator process group pgid={pgid} (pid={self.process.pid})..."
79
+ )
80
+
81
  # send SIGTERM to the whole group
82
+ try:
83
+ os.killpg(pgid, signal.SIGTERM)
84
+ except ProcessLookupError:
85
+ return
86
+
87
  try:
88
  self.process.wait(timeout=2)
89
  except subprocess.TimeoutExpired:
90
  print("force killing simulator process group...")
91
  os.killpg(pgid, signal.SIGKILL)
92
+
93
  self.process = None
94
 
95
+ return DummyEnv(proc)