-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11-virtual-server-operations.py
More file actions
60 lines (51 loc) · 1.78 KB
/
11-virtual-server-operations.py
File metadata and controls
60 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'''
Create a class VirtualServer that simulates basic virtual server operations (start, stop, reboot). Instantiate multiple servers and perform operations in parallel using threading.
'''
import threading
import time
import random
class VirtualServer:
def __init__(self, name):
self.name = name
self.status = "Stopped"
def start(self):
if self.status == "Running":
print(f"{self.name} is already running.")
return
print(f"{self.name} is starting...")
time.sleep(random.uniform(1, 2))
self.status = "Running"
print(f"{self.name} is now Running.")
def stop(self):
if self.status == "Stopped":
print(f"{self.name} is already stopped.")
return
print(f"{self.name} is stopping...")
time.sleep(random.uniform(1, 2))
self.status = "Stopped"
print(f"{self.name} is now Stopped.")
def reboot(self):
if self.status == "Stopped":
print(f"{self.name} is stopped. Starting before rebooting...")
self.start()
print(f"{self.name} is rebooting...")
time.sleep(random.uniform(1, 2))
print(f"{self.name} has rebooted.")
def run_operations(server, operation):
if operation == "start":
server.start()
elif operation == "stop":
server.stop()
elif operation == "reboot":
server.reboot()
if __name__ == "__main__":
servers = [VirtualServer(f"Server{i+1}") for i in range(3)]
operations = ["start", "stop", "reboot"]
threads = []
for i, server in enumerate(servers):
operation = operations[i]
t = threading.Thread(target=run_operations, args=(server, operation))
threads.append(t)
t.start()
for t in threads:
t.join()