python - 如何计算 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))
解决方案
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))
我希望这会对你有所帮助,因为我试图尽可能清楚地写出来
推荐阅读
- blender - Blender AL lib:UpdateDeviceParams:无法设置 44100hz,得到 48000hz,而不是 Windows
- woocommerce - 当woocommerce中产品数量超过3时如何显示错误消息?
- ios - 自动布局 UITextfield
- python - python中的函数调用和类型转换?
- java - Firebase 管理员 SDK:IllegalArgumentException,Preconditions.checkArgument
- ios - PresentViewController 不起作用:视图不在 TabBarController 的窗口层次结构中
- javascript - 在数据表中呈现列标题
- r - R:将具有相同ID的数据存储到列表中
- java - java.lang.NoSuchMethodError: cucumber.runtime.RuntimeOptions.getJunitOptions()Ljava/util/List;
- javascript - 选择具有属性 B 的元素的属性 A