Client sends and receives protobuf messages. Create and start the server, then use pull and push to communicate with the server. Attributes: global_rank (int): The rank in training process. host (str): Host address of the server. port (str): Port of the server.
| 42 | |
| 43 | |
| 44 | class Client: |
| 45 | """Client sends and receives protobuf messages. |
| 46 | |
| 47 | Create and start the server, then use pull and push to communicate with the server. |
| 48 | |
| 49 | Attributes: |
| 50 | global_rank (int): The rank in training process. |
| 51 | host (str): Host address of the server. |
| 52 | port (str): Port of the server. |
| 53 | sock (socket.socket): Socket of the client. |
| 54 | weights (Dict[Any]): Weights stored locally. |
| 55 | """ |
| 56 | |
| 57 | def __init__( |
| 58 | self, |
| 59 | global_rank: int = 0, |
| 60 | host: str = "127.0.0.1", |
| 61 | port: str = 1234, |
| 62 | ) -> None: |
| 63 | """Class init method |
| 64 | |
| 65 | Args: |
| 66 | global_rank (int, optional): The rank in training process. Defaults to 0. |
| 67 | Provided by the '-i' parameter (device_id) in the running script. |
| 68 | host (str, optional): Host ip address. Defaults to '127.0.0.1'. |
| 69 | port (str, optional): Port. Defaults to 1234. |
| 70 | """ |
| 71 | self.host = host |
| 72 | self.port = port |
| 73 | self.global_rank = global_rank |
| 74 | |
| 75 | self.sock = socket.socket() |
| 76 | |
| 77 | self.weights = {} |
| 78 | |
| 79 | def __start_connection(self) -> None: |
| 80 | """Start the network connection to server.""" |
| 81 | self.sock.connect((self.host, self.port)) |
| 82 | |
| 83 | def __start_rank_pairing(self) -> None: |
| 84 | """Sending global rank to server""" |
| 85 | utils.send_int(self.sock, self.global_rank) |
| 86 | |
| 87 | def start(self) -> None: |
| 88 | """Start the client. |
| 89 | |
| 90 | This method will first connect to the server. Then global rank is sent to the server. |
| 91 | """ |
| 92 | self.__start_connection() |
| 93 | self.__start_rank_pairing() |
| 94 | |
| 95 | print(f"[Client {self.global_rank}] Connect to {self.host}:{self.port}") |
| 96 | |
| 97 | def close(self) -> None: |
| 98 | """Close the server.""" |
| 99 | self.sock.close() |
| 100 | |
| 101 | def pull(self) -> None: |