Coding Towards The Answer, Part 9

When System Calls …

Mi'kail Eli'yah
3 min readJul 27, 2023

Let’s simulate computational nodes doing load balancing act.

import random
import matplotlib.pyplot as plt

# Constants
NUM_SERVERS = 7
NUM_REQUESTS = 10 # sessions
MAX_REQUESTS = 10

# Server class
class Server:
def __init__(self):
self.load = 0

def process_request(self):
self.load += 1

def get_load(self):
return self.load

# Initialize servers
servers = [Server() for _ in range(NUM_SERVERS)]

# Process requests
for _ in range(NUM_REQUESTS):
# Select server
# server = random.choice(servers)
# Generate a random number of requests for this iteration
num_requests = random.randint(1, MAX_REQUESTS)

# Process each request
for _ in range(num_requests):
# Select server with the minimal load
server = min(servers, key=lambda s: s.get_load())

# Process the request on the selected server
server.process_request()

# Get server loads
loads = [server.get_load() for server in servers]

# Plot graph
plt.bar(range(NUM_SERVERS), loads)
plt.xlabel('Server')
plt.ylabel('Load')
plt.title('Load…

--

--