MCPcopy Create free account
hub / github.com/apache/singa / Server

Class Server

examples/hfl/src/server.py:35–145  ·  view source on GitHub ↗

Server sends and receives protobuf messages. Create and start the server, then use pull and push to communicate with clients. Attributes: num_clients (int): Number of clients. host (str): Host address of the server. port (str): Port of the server. sock (sock

Source from the content-addressed store, hash-verified

33
34
35class Server:
36 """Server sends and receives protobuf messages.
37
38 Create and start the server, then use pull and push to communicate with clients.
39
40 Attributes:
41 num_clients (int): Number of clients.
42 host (str): Host address of the server.
43 port (str): Port of the server.
44 sock (socket.socket): Socket of the server.
45 conns (List[socket.socket]): List of num_clients sockets.
46 addrs (List[str]): List of socket address.
47 weights (Dict[Any]): Weights stored on server.
48 """
49
50 def __init__(
51 self,
52 num_clients=1,
53 host: str = "127.0.0.1",
54 port: str = 1234,
55 ) -> None:
56 """Class init method
57
58 Args:
59 num_clients (int, optional): Number of clients in training.
60 host (str, optional): Host ip address. Defaults to '127.0.0.1'.
61 port (str, optional): Port. Defaults to 1234.
62 """
63 self.num_clients = num_clients
64 self.host = host
65 self.port = port
66
67 self.sock = socket.socket()
68 self.conns = [None] * num_clients
69 self.addrs = [None] * num_clients
70
71 self.weights = {}
72
73 def __start_connection(self) -> None:
74 """Start the network connection of server."""
75 self.sock.bind((self.host, self.port))
76 self.sock.listen()
77 print("Server started.")
78
79 def __start_rank_pairing(self) -> None:
80 """Start pair each client to a global rank"""
81 for _ in range(self.num_clients):
82 conn, addr = self.sock.accept()
83 # rank is the global device_id when initializing the client
84 rank = utils.receive_int(conn)
85 self.conns[rank] = conn
86 self.addrs[rank] = addr
87 print(f"[Server] Connected by {addr} [global_rank {rank}]")
88
89 assert None not in self.conns
90
91 def start(self) -> None:
92 """Start the server.

Callers 1

server.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected