Table of Contents

Delegate AsyncVectorWorker

Namespace
Gym.Vector
Assembly
Gym.dll

A function that serves one sub-env of an AsyncVectorEnv<TObs, TAct> (DR-38, Gymnasium's worker): it builds the sub-env, then answers the parent's commands through its pipe until a close, as Gymnasium's worker process does. AsyncWorker(int, Func<IEnv>, PipeConnection, PipeConnection, object?, ConcurrentQueue<AsyncWorkerError>, AutoresetMode, SemaphoreSlim?) is the default one; a custom worker usually wraps it, changing what it needs.

public delegate void AsyncVectorWorker(int index, Func<IEnv> envFn, PipeConnection pipe, PipeConnection parentPipe, object? sharedMemory, ConcurrentQueue<AsyncWorkerError> errorQueue, AutoresetMode autoresetMode, SemaphoreSlim? semaphore)

Parameters

index int

The sub-env's index.

envFn Func<IEnv>

The function that builds the sub-env; the worker calls it and owns, closes and disposes what it returns.

pipe PipeConnection

The worker's handle on its end of the pipe: it receives (string Command, object? Data) value tuples and answers each with an (object? Result, bool Success) value tuple, in order. The commands are Gymnasium's: reset, reset-noop, step, close, _call, _setattr, _check_spaces.

parentPipe PipeConnection

The worker's own handle on the parent's end, which Gymnasium's worker closes first; closing it leaves the parent's handle open.

sharedMemory object

The shared observation buffer to write observations into (WriteToSharedMemory(Space, int, object?, object)), or null without shared memory.

errorQueue ConcurrentQueue<AsyncWorkerError>

Where a failing worker reports its exception before answering (null, false), as Gymnasium's error queue.

autoresetMode AutoresetMode

When an ended sub-env resets: the vector env's mode.

semaphore SemaphoreSlim

The max_concurrency permits shared by every worker, to hold around reset and step, or null.

Extension Methods

Remarks

The signature is Gymnasium's eight arguments, so the arity check Gymnasium runs when the env is built is a compile-time fact here. Each custom worker runs on a thread of its own (a background thread unless the vector env's daemon is off), whatever the execution mode, as each of Gymnasium's runs in a process of its own.

Footgun: unlike a process, the worker shares the parent's memory. What crosses the pipe is passed by reference, and the shared buffer is written in place (lock on sharedMemory around a write, as the default worker does, since writing touches the buffer's shared bookkeeping). A worker that returns without answering a command leaves the parent's receive to report the end of the stream (EndOfStreamException, carrying what escaped the worker, if anything).

Reference: gymnasium/vector/async_vector_env.py.