OpenEnv documentation

Async vs Sync Usage

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Async vs Sync Usage

OpenEnv supports both asynchronous and synchronous usage patterns.

When to Use Each

Pattern Best For Performance
Async Production, multiple environments, high throughput ⚡ Best
Sync Scripts, notebooks, quick experiments 🐢 Good enough

Sync Usage

For simple scripts and notebooks, use the .sync() wrapper:

from openenv import AutoEnv

env = AutoEnv.from_env("echo")

with env.sync() as client:
    result = client.reset()
    result = client.step(action)

Async Usage (Recommended)

For production and parallel environments, use async directly:

import asyncio
from openenv import AutoEnv

async def main():
    env = AutoEnv.from_env("echo")

    async with env as client:
        result = await client.reset()
        result = await client.step(action)

asyncio.run(main())

Parallel Environments

Run multiple environments concurrently:

import asyncio
from openenv import AutoEnv

async def run_episode(env_name: str):
    env = AutoEnv.from_env(env_name)
    async with env as client:
        result = await client.reset()
        # ... run episode
        return result

async def main():
    # Run 4 environments in parallel
    results = await asyncio.gather(
        run_episode("echo"),
        run_episode("echo"),
        run_episode("echo"),
        run_episode("echo"),
    )

asyncio.run(main())

Next Steps

Update on GitHub