Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算avgLoad功能,关闭连接并确保可用性功能_Python_Oop - Fatal编程技术网

Python 如何计算avgLoad功能,关闭连接并确保可用性功能

Python 如何计算avgLoad功能,关闭连接并确保可用性功能,python,oop,Python,Oop,帮我拿这一份。我试图解决这个问题,但无法执行,并已附加相同的。需要帮助计算平均负载功能、关闭连接功能和确保可用性功能。我知道这是一个作业问题,我甚至试图解决它,并已张贴了。但我还是被这些功能卡住了,如果有人能帮助我,让我提前理解并实现它,那就太好了 LoadBalancing类将仅从一台可用服务器开始。当添加连接时,它将随机选择一个服务器为该连接提供服务,然后将连接传递给服务器。LoadBalancement类还需要跟踪正在进行的连接,以便能够关闭它们 class LoadBalanci

帮我拿这一份。我试图解决这个问题,但无法执行,并已附加相同的。需要帮助计算平均负载功能、关闭连接功能和确保可用性功能。我知道这是一个作业问题,我甚至试图解决它,并已张贴了。但我还是被这些功能卡住了,如果有人能帮助我,让我提前理解并实现它,那就太好了


LoadBalancing类将仅从一台可用服务器开始。当添加连接时,它将随机选择一个服务器为该连接提供服务,然后将连接传递给服务器。LoadBalancement类还需要跟踪正在进行的连接,以便能够关闭它们

    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))
应添加以下代码:

"server.add_connection(connection_id)"
应添加以下代码:

"server.add_connection(connection_id)"
Self.servers是一个列表,不能将其添加到数字(sum)中,因此您应该参考赋值的第一部分中的load()函数:

 sum += server.load()
Self.servers是一个列表,不能将其添加到数字(sum)中,因此您应该参考赋值的第一部分中的load()函数:

 sum += server.load()
在这里,您没有放置任何代码来启动新服务器:

 if self.avg_load() > 50:
        self.servers.extend([Server()])
在这里,您没有放置任何代码来启动新服务器:

 if self.avg_load() > 50:
        self.servers.extend([Server()])
我希望这会对你有所帮助,因为我尽力把它写得尽可能清楚


我希望这能对你有所帮助,因为我尽量把它写得清晰

你能提供以下方面的更多信息吗-1。平均负载的定义是什么?这是每台服务器总连接数的平均值,即总连接数/服务器数吗?如果这是真的,那么不同的服务器会有不同的负载,因此负载平衡将是一个困难的问题。2.Close_connections需要从self.connections中删除已关闭的连接。这不是因为任何特定的原因吗?负载平衡类将只从一台可用的服务器开始。当添加连接时,它将随机选择一个服务器为该连接提供服务,然后将连接传递给服务器。LoadBalancing类还需要跟踪正在进行的连接,以便能够关闭它们。请提供以下-1的更多信息。平均负载的定义是什么?这是每台服务器总连接数的平均值,即总连接数/服务器数吗?如果这是真的,那么不同的服务器会有不同的负载,因此负载平衡将是一个困难的问题。2.Close_connections需要从self.connections中删除已关闭的连接。这不是因为任何特定的原因吗?负载平衡类将只从一台可用的服务器开始。当添加连接时,它将随机选择一个服务器为该连接提供服务,然后将连接传递给服务器。LoadBalancement类还需要跟踪正在进行的连接,以便能够关闭它们。
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))