Implementation:Farama Foundation Gymnasium AsyncVectorEnv
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Reinforcement_Learning, Parallelism |
| Last Updated | 2026-02-15 03:00 GMT |
Overview
Concrete tool for running multiple environments in parallel via multiprocessing provided by the Gymnasium library.
Description
AsyncVectorEnv spawns each sub-environment in a separate process, using multiprocessing.Pipe for communication and optionally multiprocessing.shared_memory for zero-copy observation transfer. It supports configurable process context (fork/spawn/forkserver) and daemon mode.
Usage
Use AsyncVectorEnv directly with custom environment factories or via gymnasium.make_vec with vectorization_mode="async". Enable shared_memory=True (default) for large observations like images.
Code Reference
Source Location
- Repository: Gymnasium
- File: gymnasium/vector/async_vector_env.py
- Lines: L54-120
Signature
class AsyncVectorEnv(VectorEnv):
def __init__(
self,
env_fns: Sequence[Callable[[], Env]],
shared_memory: bool = True,
copy: bool = True,
context: str | None = None,
daemon: bool = True,
worker: Callable | None = None,
observation_mode: str | Space = "same",
autoreset_mode: str | AutoresetMode = AutoresetMode.NEXT_STEP,
):
"""Vectorized environment that runs multiple environments in parallel.
Args:
env_fns: Functions that create the environments.
shared_memory: Use shared memory for observations (default True).
copy: Return copies of observations (default True).
context: Multiprocessing context ("fork", "spawn", or "forkserver").
daemon: Whether subprocesses are daemon processes (default True).
worker: Optional custom worker function.
observation_mode: "same" or "different".
autoreset_mode: How to handle automatic resets.
"""
Import
from gymnasium.vector import AsyncVectorEnv
import gymnasium as gym
envs = AsyncVectorEnv([
lambda: gym.make("LunarLander-v3"),
lambda: gym.make("LunarLander-v3"),
])
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| env_fns | Sequence[Callable] | Yes | Factory functions creating environments |
| shared_memory | bool | No | Use shared memory for observations (default True) |
| copy | bool | No | Copy observations (default True) |
| context | str or None | No | Multiprocessing context |
| daemon | bool | No | Daemon subprocesses (default True) |
Outputs
| Name | Type | Description |
|---|---|---|
| num_envs | int | Number of parallel environments |
| single_observation_space | Space | Single env observation space |
| single_action_space | Space | Single env action space |
Usage Examples
Async Training Setup
import gymnasium as gym
# Create async vectorized environments
envs = gym.make_vec(
"LunarLander-v3",
num_envs=16,
vectorization_mode="async",
)
obs, infos = envs.reset(seed=42)
print(obs.shape) # (16, 8)
for step in range(1000):
actions = envs.action_space.sample()
obs, rewards, terms, truncs, infos = envs.step(actions)
envs.close()
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment