首页 > 解决方案 > 如何计算 avgLoad 函数,关闭连接并确保可用性函数

问题描述

帮我解决这部分。我尝试解决此问题,但无法执行并附加了相同的内容。在计算平均负载函数、关闭连接函数和确保可用性函数时需要帮助。我知道这是一个作业问题,我什至尝试解决它并发布了它。但是我仍然卡在这些功能上,如果有人可以帮助我,这样我就可以理解并提前实现它,那就太好了。

LoadBalancing 类将从只有一台可用的服务器开始。添加连接后,它将随机选择一个服务器来提供该连接,然后将连接传递给服务器。LoadBalancing 类还需要跟踪正在进行的连接才能关闭它们。

    class LoadBalancing:
def __init__(self):
    """Initialize the load balancing system with one server"""
    self.connections = {}
    self.servers = [Server()]

def add_connection(self, connection_id):
    """Randomly selects a server and adds a connection to it."""
    server = random.choice(self.servers)
    # Add the connection to the dictionary with the selected server
    # Add the connection to the server
    self.connections[connection_id] = server

def close_connection(self, connection_id):
    """Closes the connection on the server corresponding to connection_id."""
    # Find out the right server
    # Close the connection on the server
    # Remove the connection from the load balancer

def avg_load(self):
    """Calculates the average load of all servers"""
    # Sum the load of each server and divide by the amount of servers
    sum = 0
    for server in self.servers:
        sum += self.servers
    return sum/len(self.servers)

def ensure_availability(self):
    """If the average load is higher than 50, spin up a new server"""
    pass

def __str__(self):
    """Returns a string with the load for each server."""
    loads = [str(server) for server in self.servers]
    return "[{}]".format(",".join(loads))

标签: pythonoop

解决方案


class LoadBalancing:
def __init__(self):
    """Initialize the load balancing system with one server"""
    self.connections = {}
    self.servers = [Server()]

def add_connection(self, connection_id):
    """Randomly selects a server and adds a connection to it."""
    server = random.choice(self.servers)
    # Add the connection to the dictionary with the selected server
    # Add the connection to the server
    self.connections[connection_id] = server
    server.add_connection(connection_id)
    self.ensure_availability()
    

def close_connection(self, connection_id):
    """Closes the connection on the the server corresponding to connection_id."""
    # Find out the right server
    # Close the connection on the server
    # Remove the connection from the load balancer
    for connection in self.connections.keys():
        if connection == connection_id:
            server_of_connection = self.connections[connection]
    server_of_connection.close_connection(connection_id)
    del self.connections[connection_id]
    

def avg_load(self):
    """Calculates the average load of all servers"""
    # Sum the load of each server and divide by the amount of servers
    result = 0
    loads = 0
    for server in self.servers:
        result += server.load()
        loads += 1
    return result/loads

def ensure_availability(self):
    """If the average load is higher than 50, spin up a new server"""
    if self.avg_load() >= 50:
        self.servers.append(Server())

def __str__(self):
    """Returns a string with the load for each server."""
    loads = [str(server) for server in self.servers]
    return "[{}]".format(",".join(loads))

我希望这会对你有所帮助,因为我试图尽可能清楚地写出来


推荐阅读