-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (45 loc) · 1.25 KB
/
main.py
File metadata and controls
60 lines (45 loc) · 1.25 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
from flask import Flask, request
import time
import os
from multiprocessing import Process, Value
from pynvml import *
app = Flask(__name__)
start = time.time()
stop = Value('i', False)
def getUtilization(handle):
try:
util = nvmlDeviceGetMemoryInfo(handle)
mem = util.used
except NVMLError as error:
print(error)
mem = -1
return mem
def monitor_utilization(target):
nvmlInit()
with open(target, 'w') as f:
f.write("mem_util\n")
while not stop.value:
handle = nvmlDeviceGetHandleByIndex(0)
mem_util = getUtilization(handle)
f.write("%s\n" % (mem_util / 1024 / 1024))
f.flush()
time.sleep(1)
nvmlShutdown()
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app.route('/time')
def timer():
return str(time.time() - start)
@app.route('/exit')
def exit():
shutdown_server()
return 'done'
if __name__ == '__main__':
p = Process(target=monitor_utilization, args=('/utilization/log.csv',))
p.start()
app.run(debug=False, host="0.0.0.0", port=5000)
stop.value = True
p.join()