Closed tarcey closed 3 years ago
If you want to run multiple environments in parallel, you could give AsyncVectorEnv
a try. This runs each environment in a separate process though, not in a separate thread. Here is an example of how to use it:
import gym
envs = gym.vector.AsyncVectorEnv([lambda: gym.make('CartPole-v0')] * 64)
# Or alternatively
# envs = gym.vector.make('CartPole-v0', num_envs=64)
And then it behaves like any Gym environment
observations = envs.reset() # Shape (64, 4)
for _ in range(10):
actions = envs.action_space.sample() # Or your policy
observations, rewards, dones, infos = envs.step(actions)
Here is some documentation about the VectorEnv
API if you want to have a look at that. There is no out of the box equivalent for vectorized environments using threads in Gym unfortunately, but there is this PR https://github.com/openai/baselines/pull/958 that was open for baselines that introduced a similar vectorized environment with threads.
Thank you for the quick and helpful response, i will try that.
I want to run multiple separate environments of the same kind in parallel. Each environment is ever only called by one thread each, and runs independent of all other instances. Per code, each environment should be called correctly and with correct synchronization (automatic as in joblib's 'embarrassingly parallel' use-case), however the reset-function does not register (despite returning an initial observation) when called asynchronously on multiple environments in parallel.
Assuming that each env environment is entirely independent of all others and there are no thread-local variables, i would think that the code below would work. Is my assumption incorrect? What is the correct way to run multiple environments in parallel in gym?
Runnable example code below. With n_jobs=1 no problem occurs. When only the step-function is called in parallel, but the reset function is called iteratively in plain python, no error is thrown. But i'm hesitant to trust the output.