Closed LiuTed closed 3 years ago
I was not able to reproduce this error, but when you give a list to wrappers
in gym.vector.make
, this applies the sequence of wrappers to all the sub-environments. Here, this means that for both sub-environments, you apply Monitor
twice; in other words, all the sub-environments will be instantiated like
Monitor(Monitor(gym.make('CartPole-v0'), 'cartpole-log-0'), 'cartpole-log-1')
I don't know if this explains your issue though sorry. This worked for me (tested it on MacOS):
import gym
import pyglet.gl # or import gym.envs.classic_control.rendering
def make_env(i):
def _make():
env = gym.make('CartPole-v0')
env = gym.wrappers.Monitor(env, f'cartpole-log-{i}', force=True)
return env
return _make
env = gym.vector.AsyncVectorEnv([make_env(i) for i in range(2)])
env.reset()
I was not able to reproduce this error, but when you give a list to
wrappers
ingym.vector.make
, this applies the sequence of wrappers to all the sub-environments. Here, this means that for both sub-environments, you applyMonitor
twice; in other words, all the sub-environments will be instantiated likeMonitor(Monitor(gym.make('CartPole-v0'), 'cartpole-log-0'), 'cartpole-log-1')
I don't know if this explains your issue though sorry. This worked for me (tested it on MacOS):
import gym import pyglet.gl # or import gym.envs.classic_control.rendering def make_env(i): def _make(): env = gym.make('CartPole-v0') env = gym.wrappers.Monitor(env, f'cartpole-log-{i}', force=True) return env return _make env = gym.vector.AsyncVectorEnv([make_env(i) for i in range(2)]) env.reset()
Thanks for your notice about the vector.make
, but this is inrelevent to the problem. I am sorry for not being able to reproduce this problem, maybe it is caused by the implementation of X server. Thanks for your reply.
Alternatively it could be because Linux by default uses the start method fork
for multiprocessing, which may not play well with pyglet. You can try to set the start method to something else, e.g. spawn
, with
if __name__ == '__main__':
env = gym.vector.AsyncVectorEnv([make_env(i) for i in range(2)], context='spawn')
env.reset()
You would have to use the AsyncVectorEnv
API for changing the context, this is not accessible from gym.vector.make
. Don't forget to wrap your code containing the vectorized environment in if __name__ == '__main__':
, see this documentation on multiprocessing for more information.
After switching the context to 'spawn', this problem seems to be fixed. Thanks a lot.
I was trying to vectorize my customized environment, which imported
gym.envs.classic_control.rendering
globally, usinggym.vector.make
. However, I got errors like the following lines when reseting the envrionment, even using CartPole-v0:This error can be easily reproduced with the following codes:
If commanded out the line 3:
import pyglet.gl
, this error will not happen. This problem will only happens iff usingAsyncVectorEnv
withnum_envs>1
, and usingMonitor
wrapper. Ifnum_envs=1
orasynchronous=False
, or simply not usingMonitor
, this will not happen.I am wondering why such error will occur, and if it is possible to warn such usage (import rendering globally).
I executed the code above in Python 3.8 with libraries gym==0.19.0, pyglet==1.5.0. OS is Ubuntu 20.04 in WSL2. X Server is VcXsrv 1.20.8.1.