-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
50 lines (37 loc) · 1.28 KB
/
serve.py
File metadata and controls
50 lines (37 loc) · 1.28 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
"""
HTTP POST "/"
Once receive the POST request, your server program should create a
separate process for running "stress_cpu.py" (attached below).
"stress_cpu.py" runs an intensive computation loop to stress the
CPUs to 100% utilization. This script is for triggering the auto
scaling policy that you will setup later.
In Python, use subprocess.Popen() to create a separate process so
that your HTTP server is non-blocking. That is to say, after
receiving a POST request for stressing the CPU on your EC2
instance, your server program should still be able to handle
further POST/GET requests.
HTTP GET "/"
Your server program should return the private IP address of the
EC2 instance. In Python, you can import socket and use
socket.gethostname() to get the IP address.
"""
from flask import Flask, request
import socket
import subprocess
app = Flask(__name__)
@app.route('/', methods = ['POST'])
def seeding():
subprocess.Popen(["python3" ,"stress_cpu.py"])
return ""
@app.route('/', methods = ['GET'])
def get_ip():
return socket.gethostname()
@app.route('/health')
def check_health():
try:
with open(seed_file,"r") as f:
return ""
except:
return "Health check failed"
if __name__ == '__main__':
app.run(host="0.0.0.0",port=80)