subproc#


class ShArray(dtype: generic, shape: tuple[int], ctx: BaseContext | None)[source]#

Wrapper of multiprocessing Array.

Example usage:

import numpy as np
import multiprocessing as mp
from tianshou.env.worker.subproc import ShArray
ctx = mp.get_context('fork')  # set an explicit context
arr = ShArray(np.dtype(np.float32), (2, 3), ctx)
arr.save(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32))
print(arr.get())
get() ndarray[source]#
save(ndarray: ndarray) None[source]#
class SubprocEnvWorker(env_fn: Callable[[], Env], share_memory: bool = False, context: BaseContext | Literal['fork', 'spawn'] | None = None)[source]#

Subprocess worker used in SubprocVectorEnv and ShmemVectorEnv.

close_env() None[source]#
get_env_attr(key: str) Any[source]#
recv() tuple[ndarray, ndarray, ndarray, ndarray, ndarray] | tuple[ndarray, dict][source]#

Receive result from low-level worker.

If the last “send” function sends a NULL action, it only returns a single observation; otherwise it returns a tuple of (obs, rew, done, info) or (obs, rew, terminated, truncated, info), based on whether the environment is using the old step API or the new one.

render(**kwargs: Any) Any[source]#

Render the environment.

reset(**kwargs: Any) tuple[ndarray, dict][source]#
seed(seed: int | None = None) list[int] | None[source]#
send(action: ndarray | None, **kwargs: Any) None[source]#

Send action signal to low-level worker.

When action is None, it indicates sending “reset” signal; otherwise it indicates “step” signal. The paired return value from “recv” function is determined by such kind of different signal.

set_env_attr(key: str, value: Any) None[source]#
static wait(workers: list[SubprocEnvWorker], wait_num: int, timeout: float | None = None) list[SubprocEnvWorker][source]#

Given a list of workers, return those ready ones.