-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_gpu.py
45 lines (42 loc) · 1.32 KB
/
node_gpu.py
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
from detect import *
import time
import os
import fcntl
import json
import pickle
import sys
period = 5
if __name__ == '__main__':
#get hostname
node_name = os.popen('hostname').readline().strip()
home = os.path.expanduser('~')
#filename = '/home/yanshp/gpustat/{}_gpu_stat.pickle'.format(node_name)
filename = os.path.join(home, 'gpustat/{}_gpu_stat.json'.format(node_name))
while True:
lock_file = filename + '.lock'
while os.path.exists(lock_file):
time.sleep(0.1)
print("host_name:%s open file" % node_name)
try:
with open(filename,'w') as f:
open(lock_file, 'w+').close()
# import pdb;pdb.set_trace()
contents = query_gpu()
timestamp = time.time()
result ={
'timestamp': timestamp,
'gpus_stats': contents,
}
#import pdb;pdb.set_trace()
json.dump(result, f)
print("update successfully")
except KeyboardInterrupt as e:
os.remove(lock_file)
print("exiting .......")
sys.exit(0)
except IOError as e:
pass
finally:
time.sleep(0.5)
os.remove(lock_file)
time.sleep(period)