Lee el código con atención
apps = ["app1", "app2", "app3"]
# Round-robin: cycle through apps in order.
class RoundRobin:
def __init__(self, apps):
self.apps = apps
self.i = 0
def pick(self, ip):
choice = self.apps[self.i % len(self.apps)]
self.i += 1
return choice
# IP-hash: same ip ALWAYS lands on the same app — sticky sessions.
# We use sum(ord(c)) instead of hash() so the answer is reproducible
# across Python versions (CPython hash() is randomised per process).
class IPHash:
def __init__(self, apps):
self.apps = apps
def pick(self, ip):
return self.apps[sum(ord(c) for c in ip) % len(self.apps)]
rr = RoundRobin(apps)
print(rr.pick("10.0.0.1")) # request 1 — cycle position 0
print(rr.pick("10.0.0.2")) # request 2 — cycle position 1
ih = IPHash(apps)
print(ih.pick("10.0.0.5")) # same IP twice — should pin
print(ih.pick("10.0.0.5"))
# What does this print? Type your prediction.¿Qué imprimirá el programa? Escribe aquí: