ServerApp

class ServerApp(server: Server | None = None, config: ServerConfig | None = None, strategy: Strategy | None = None, client_manager: ClientManager | None = None, server_fn: Callable[[Context], ServerAppComponents] | None = None)[source]

Bases : object

Flower ServerApp.

Exemples

Use the ServerApp with an existing Strategy:

def server_fn(context: Context):
    server_config = ServerConfig(num_rounds=3)
    strategy = FedAvg()
    return ServerAppComponents(
        strategy=strategy,
        server_config=server_config,
    )

app = ServerApp(server_fn=server_fn)

Use the ServerApp with a custom main function:

app = ServerApp()

@app.main()
def main(grid: Grid, context: Context) -> None:
   print("ServerApp running")

Methods

lifespan()

Return a decorator that registers the lifespan fn with the server app.

main()

Return a decorator that registers the main fn with the server app.

lifespan() Callable[[Callable[[Context], Iterator[None]]], Callable[[Context], Iterator[None]]][source]

Return a decorator that registers the lifespan fn with the server app.

The decorated function should accept a Context object and use yield to define enter and exit behavior.

Exemples

app = ServerApp()

@app.lifespan()
def lifespan(context: Context) -> None:
    # Perform initialization tasks before the app starts
    print("Initializing ServerApp")

    yield  # ServerApp is running

    # Perform cleanup tasks after the app stops
    print("Cleaning up ServerApp")
main() Callable[[Callable[[Grid, Context], None]], Callable[[Grid, Context], None]][source]

Return a decorator that registers the main fn with the server app.

Exemples

app = ServerApp()

@app.main()
def main(grid: Grid, context: Context) -> None:
    print("ServerApp running")