TensorDictServer#
- class tensordict._ucxx.TensorDictServer(port: int, *, consolidated: bool = True)#
Bases:
objectA listener that accepts several clients, and gives a
TensorDictPipefor each.Warning
TensorDictServeris experimental: its API can change in a minor release without a deprecation cycle.It needs the optional
ucxxpackage. Import it fromtensordict._ucxx: there is no public import path. Get the pipes withserve(), or withasync for pipe in server. It starts listening when you do either. The protocol does not authenticate the clients: use it only on a trusted network. A server is an async context manager, which closes it at the end of the block.- Parameters:
port (int) – the port to listen on.
- Keyword Arguments:
consolidated (bool, optional) – the
consolidatedargument of the pipes (seeTensorDictPipe). The clients must pass the same value toTensorDictPipe.connect(). Defaults toTrue.- Raises:
ImportError – if
ucxxis not installed.
Examples
Each client connects with
TensorDictPipe.connect(). This example is not tested: it needsucxxand client processes.import asyncio from tensordict._ucxx import TensorDictServer async def handler(pipe): async for td in pipe: print(td["obs"].shape) async def main(): server = TensorDictServer(port=13337) await server.serve(handler) # runs until server.close() asyncio.run(main())
- close() None#
Closes the server: it stops accepting connections, and
serve()returns.The pipes of the clients stay open.
- async serve(on_connect: Callable[[TensorDictPipe], Awaitable[None]]) None#
Accepts connections, and runs
on_connecton the pipe of each new client.Runs until
close()is called. Eachon_connectcall runs as its own task, so that the clients are served at the same time.async for pipe in serveris the other way to get the pipes.- Parameters:
on_connect (Callable) – a coroutine function that takes the
TensorDictPipeof a client.