# Only use Ray to vectorize environment

**URL:** https://discuss.ray.io/t/only-use-ray-to-vectorize-environment/2893
**Category:** RLlib
**Created:** [July 14, 2021, 9:58am UTC](https://discuss.ray.io/t/only-use-ray-to-vectorize-environment/2893 "2021-07-14T09:58:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ingambe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ingambe/32/52_2.png) [@ingambe](https://discuss.ray.io/u/ingambe)
#### Post date: [July 14, 2021, 9:58am UTC](https://discuss.ray.io/t/only-use-ray-to-vectorize-environment/2893/1 "2021-07-14T09:58:09Z")

</div>

I want to vectorize my env using ray, ideally getting something like `AsyncVecEnv` from OpenAi’s gym but using ray under the hood rather than python’s multiprocessing  
Is it possible to do so?  
Thanks in advance

---

<div class="post-metadata">

### Author: ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)
#### Post date: [July 14, 2021, 3:35pm UTC](https://discuss.ray.io/t/only-use-ray-to-vectorize-environment/2893/2 "2021-07-14T15:35:24Z")

</div>

Hey @ingambe ,

There are two ways to vectorize your custom (e.g. gym) envs using RLlib:

- serial: This is the default. RLlib will create n sub-envs (all instances of your custom env) and step through these in sequence, then batching the resulting next observations for the next action computing forward pass.
- parallel: Set `remote_worker_envs=True` in your config. This will create n of your custom (gym) env instances and wrap each to become a `@ray.remote` actor. Stepping through the n sub-envs is then done in parallel.

---

<div class="post-metadata">

### Author: ![ingambe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ingambe/32/52_2.png) [@ingambe](https://discuss.ray.io/u/ingambe)
#### Post date: [July 15, 2021, 9:48am UTC](https://discuss.ray.io/t/only-use-ray-to-vectorize-environment/2893/3 "2021-07-15T09:48:13Z")

</div>

Thank you @sven1977  
Is there a possibility to use this vectorized environment outside of RLLib?  
I would like to write my own custom algorithm and use the vectorized environment for better performance (the MPI version is really bad performance-wise)

---

<div class="post-metadata">

### Author: ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)
#### Post date: [July 15, 2021, 10:08am UTC](https://discuss.ray.io/t/only-use-ray-to-vectorize-environment/2893/4 "2021-07-15T10:08:06Z")

</div>

You could take a look at how RemoteVectorEnv in RLlib is being “stepped”. It’s basically just a ray.remote call on the individual envs’ reset/step methods and then collecting the results via `ray.get`.

The code is in here:  
`ray.rllib.env.remote_vector_env.py::RemoteVectorEnv::poll`

It’s a little different from the gym API, as we support async polling of our envs.

---

<div class="post-metadata">

### Author: ![ingambe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ingambe/32/52_2.png) [@ingambe](https://discuss.ray.io/u/ingambe)
#### Post date: [July 15, 2021, 10:35am UTC](https://discuss.ray.io/t/only-use-ray-to-vectorize-environment/2893/5 "2021-07-15T10:35:25Z")

</div>

Thanks a lot @sven1977 !  
That’s exactly what I needed 😀
