class_docstr: Vectorized environment that runs multiple environments in parallel.
  
  It uses ``multiprocessing`` processes, and pipes for communication.
  
  Example::
  
      >>> import gym
      >>> env = gym.vector.AsyncVectorEnv([
      ...     lambda: gym.make("Pendulum-v0", g=9.81),
      ...     lambda: gym.make("Pendulum-v0", g=1.62)
      ... ])
      >>> env.reset()
      array([[-0.8286432 ,  0.5597771 ,  0.90249056],
             [-0.85009176,  0.5266346 ,  0.60007906]], dtype=float32)
class_docstr_tok: ["Vectorized","environment","that","runs","multiple","environments","in","parallel",".","It","uses","``","multiprocessing","``","processes",",","and","pipes","for","communication",".","Example",":",":",">",">",">","import","gym",">",">",">","env","=","gym.vector.AsyncVectorEnv","(","[","...","lambda",":","gym.make","(","``","Pendulum-v0","''",",","g=9.81",")",",","...","lambda",":","gym.make","(","``","Pendulum-v0","''",",","g=1.62",")","...","]",")",">",">",">","env.reset","(",")","array","(","[","[","-0.8286432",",","0.5597771",",","0.90249056","]",",","[","-0.85009176",",","0.5266346",",","0.60007906","]","]",",","dtype=float32",")"]
docstr: Vectorized environment that runs multiple environments in parallel.
  
  Args:
      env_fns: Functions that create the environments.
      observation_space: Observation space of a single environment. If ``None``,
          then the observation space of the first environment is taken.
      action_space: Action space of a single environment. If ``None``,
          then the action space of the first environment is taken.
      shared_memory: If ``True``, then the observations from the worker processes are communicated back through
          shared variables. This can improve the efficiency if the observations are large (e.g. images).
      copy: If ``True``, then the :meth:`~AsyncVectorEnv.reset` and :meth:`~AsyncVectorEnv.step` methods
          return a copy of the observations.
      context: Context for `multiprocessing`_. If ``None``, then the default context is used.
      daemon: If ``True``, then subprocesses have ``daemon`` flag turned on; that is, they will quit if
          the head process quits. However, ``daemon=True`` prevents subprocesses to spawn children,
          so for some environments you may want to have it set to ``False``.
      worker: If set, then use that worker in a subprocess instead of a default one.
          Can be useful to override some inner vector env logic, for instance, how resets on termination or truncation are handled.
  
  Warnings: worker is an advanced mode option. It provides a high degree of flexibility and a high chance
      to shoot yourself in the foot; thus, if you are writing your own worker, it is recommended to start
      from the code for ``_worker`` (or ``_worker_shared_memory``) method, and add changes.
  
  Raises:
      RuntimeError: If the observation space of some sub-environment does not match observation_space
          (or, by default, the observation space of the first sub-environment).
      ValueError: If observation_space is a custom space (i.e. not a default space in Gym,
          such as gym.spaces.Box, gym.spaces.Discrete, or gym.spaces.Dict) and shared_memory is True.
docstr_tok: ["Vectorized","environment","that","runs","multiple","environments","in","parallel",".","Args",":","env_fns",":","Functions","that","create","the","environments",".","observation_space",":","Observation","space","of","a","single","environment",".","If","``","None","``",",","then","the","observation","space","of","the","first","environment","is","taken",".","action_space",":","Action","space","of","a","single","environment",".","If","``","None","``",",","then","the","action","space","of","the","first","environment","is","taken",".","shared_memory",":","If","``","True","``",",","then","the","observations","from","the","worker","processes","are","communicated","back","through","shared","variables",".","This","can","improve","the","efficiency","if","the","observations","are","large","(","e.g",".","images",")",".","copy",":","If","``","True","``",",","then","the",":","meth",":","`","~AsyncVectorEnv.reset","`","and",":","meth",":","`","~AsyncVectorEnv.step","`","methods","return","a","copy","of","the","observations",".","context",":","Context","for","`","multiprocessing","`","_",".","If","``","None","``",",","then","the","default","context","is","used",".","daemon",":","If","``","True","``",",","then","subprocesses","have","``","daemon","``","flag","turned","on",";","that","is",",","they","will","quit","if","the","head","process","quits",".","However",",","``","daemon=True","``","prevents","subprocesses","to","spawn","children",",","so","for","some","environments","you","may","want","to","have","it","set","to","``","False","``",".","worker",":","If","set",",","then","use","that","worker","in","a","subprocess","instead","of","a","default","one",".","Can","be","useful","to","override","some","inner","vector","env","logic",",","for","instance",",","how","resets","on","termination","or","truncation","are","handled",".","Warnings",":","worker","is","an","advanced","mode","option",".","It","provides","a","high","degree","of","flexibility","and","a","high","chance","to","shoot","yourself","in","the","foot",";","thus",",","if","you","are","writing","your","own","worker",",","it","is","recommended","to","start","from","the","code","for","``","_worker","``","(","or","``","_worker_shared_memory","``",")","method",",","and","add","changes",".","Raises",":","RuntimeError",":","If","the","observation","space","of","some","sub-environment","does","not","match","observation_space","(","or",",","by","default",",","the","observation","space","of","the","first","sub-environment",")",".","ValueError",":","If","observation_space","is","a","custom","space","(","i.e",".","not","a","default","space","in","Gym",",","such","as","gym.spaces.Box",",","gym.spaces.Discrete",",","or","gym.spaces.Dict",")","and","shared_memory","is","True","."]
code: def __init__(
      self,
      env_fns: Sequence[callable],
      observation_space: Optional[gym.Space] = None,
      action_space: Optional[gym.Space] = None,
      shared_memory: bool = True,
      copy: bool = True,
      context: Optional[str] = None,
      daemon: bool = True,
      worker: Optional[callable] = None,
  ):
      """Vectorized environment that runs multiple environments in parallel.
  
      Args:
          env_fns: Functions that create the environments.
          observation_space: Observation space of a single environment. If ``None``,
              then the observation space of the first environment is taken.
          action_space: Action space of a single environment. If ``None``,
              then the action space of the first environment is taken.
          shared_memory: If ``True``, then the observations from the worker processes are communicated back through
              shared variables. This can improve the efficiency if the observations are large (e.g. images).
          copy: If ``True``, then the :meth:`~AsyncVectorEnv.reset` and :meth:`~AsyncVectorEnv.step` methods
              return a copy of the observations.
          context: Context for `multiprocessing`_. If ``None``, then the default context is used.
          daemon: If ``True``, then subprocesses have ``daemon`` flag turned on; that is, they will quit if
              the head process quits. However, ``daemon=True`` prevents subprocesses to spawn children,
              so for some environments you may want to have it set to ``False``.
          worker: If set, then use that worker in a subprocess instead of a default one.
              Can be useful to override some inner vector env logic, for instance, how resets on termination or truncation are handled.
  
      Warnings: worker is an advanced mode option. It provides a high degree of flexibility and a high chance
          to shoot yourself in the foot; thus, if you are writing your own worker, it is recommended to start
          from the code for ``_worker`` (or ``_worker_shared_memory``) method, and add changes.
  
      Raises:
          RuntimeError: If the observation space of some sub-environment does not match observation_space
              (or, by default, the observation space of the first sub-environment).
          ValueError: If observation_space is a custom space (i.e. not a default space in Gym,
              such as gym.spaces.Box, gym.spaces.Discrete, or gym.spaces.Dict) and shared_memory is True.
      """
      ctx = mp.get_context(context)
      self.env_fns = env_fns
      self.shared_memory = shared_memory
      self.copy = copy
      dummy_env = env_fns[0]()
      self.metadata = dummy_env.metadata
  
      if (observation_space is None) or (action_space is None):
          observation_space = (
              observation_space or dummy_env.observation_space
          )
          action_space = action_space or dummy_env.action_space
      dummy_env.close()
      del dummy_env
      super().__init__(
          num_envs=len(env_fns),
          observation_space=observation_space,
          action_space=action_space,
      )
  
      if self.shared_memory:
          try:
              _obs_buffer = create_shared_memory(
                  self.single_observation_space,
                  n=self.num_envs,
                  ctx=ctx,
              )
              self.observations = read_from_shared_memory(
                  self.single_observation_space,
                  _obs_buffer,
                  n=self.num_envs,
              )
          except CustomSpaceError:
              raise ValueError(
                  "Using `shared_memory=True` in `AsyncVectorEnv` "
                  "is incompatible with non-standard Gym observation spaces "
                  "(i.e. custom spaces inheriting from `gym.Space`), and is "
                  "only compatible with default Gym spaces (e.g. `Box`, "
                  "`Tuple`, `Dict`) for batching. Set `shared_memory=False` "
                  "if you use custom observation spaces."
              )
      else:
          _obs_buffer = None
          self.observations = create_empty_array(
              self.single_observation_space,
              n=self.num_envs,
              fn=np.zeros,
          )
  
      self.parent_pipes, self.processes = [], []
      self.error_queue = ctx.Queue()
      target = _worker_shared_memory if self.shared_memory else _worker
      target = worker or target
      with clear_mpi_env_vars():
          for idx, env_fn in enumerate(self.env_fns):
              parent_pipe, child_pipe = ctx.Pipe()
              process = ctx.Process(
                  target=target,
                  name=f"Worker<{type(self).__name__}>-{idx}",
                  args=(
                      idx,
                      CloudpickleWrapper(env_fn),
                      child_pipe,
                      parent_pipe,
                      _obs_buffer,
                      self.error_queue,
                  ),
              )
  
              self.parent_pipes.append(parent_pipe)
              self.processes.append(process)
  
              process.daemon = daemon
              process.start()
              child_pipe.close()
  
      self._state = AsyncState.DEFAULT
      self._check_spaces()
  
imports: import multiprocessing
  import sys
  import time
  from copy import deepcopy
  from enum import Enum
  from typing import List, Optional, Sequence, Tuple, Union
  import numpy
  import gym
  from gym import logger
  from gym.core import ObsType
  from gym.error import AlreadyPendingCallError, ClosedEnvironmentError, CustomSpaceError, NoAsyncCallError
  from gym.vector.utils import CloudpickleWrapper, clear_mpi_env_vars, concatenate, create_empty_array, create_shared_memory, iterate, read_from_shared_memory, write_to_shared_memory
  from gym.vector.vector_env import VectorEnv
label_desc: Use image node_id 1 to create a new AsyncVectorEnv object from inherited base classes: VectorEnv with example: obj = AsyncVectorEnv(env_fns, observation_space, action_space, shared_memory, copy, context, daemon, worker)