@@ -17,6 +17,7 @@ from visualdl.server import lib
1717from visualdl .server .log import logger
1818from visualdl .server .mock import data as mock_data
1919from visualdl .server .mock import data as mock_tags
20+ from visualdl .python .cache import MemCache
2021from visualdl .python .storage import (LogWriter , LogReader )
2122
2223app = Flask (__name__ , static_url_path = "" )
@@ -33,7 +34,7 @@ def try_call(function, *args, **kwargs):
3334 res = lib .retry (error_retry_times , function , error_sleep_time , * args ,
3435 ** kwargs )
3536 if not res :
36- raise exceptions . IOError ("server IO error, will retry latter." )
37+ logger . error ("server temporary error, will retry latter." )
3738 return res
3839
3940
@@ -70,6 +71,14 @@ def parse_args():
7071 action = "store" ,
7172 dest = "logdir" ,
7273 help = "log file directory" )
74+ parser .add_argument (
75+ "--cache_timeout" ,
76+ action = "store" ,
77+ dest = "cache_timeout" ,
78+ type = float ,
79+ default = 20 ,
80+ help = "memory cache timeout duration in seconds, default 20" ,
81+ )
7382 args = parser .parse_args ()
7483 if not args .logdir :
7584 parser .print_help ()
@@ -86,8 +95,11 @@ log_reader = LogReader(args.logdir)
8695
8796# mannully put graph's image on this path also works.
8897graph_image_path = os .path .join (args .logdir , 'graph.jpg' )
98+ # use a memory cache to reduce disk reading frequency.
99+ CACHE = MemCache (timeout = args .cache_timeout )
100+ cache_get = lib .cache_get (CACHE )
101+
89102
90- # return data
91103# status, msg, data
92104def gen_result (status , msg , data ):
93105 """
@@ -126,52 +138,54 @@ def logdir():
126138
127139@app .route ('/data/runs' )
128140def runs ():
129- result = gen_result (0 , "" , lib .get_modes (log_reader ))
141+ data = cache_get ('/data/runs' , lib .get_modes , log_reader )
142+ result = gen_result (0 , "" , data )
130143 return Response (json .dumps (result ), mimetype = 'application/json' )
131144
132145
133146@app .route ("/data/plugin/scalars/tags" )
134147def scalar_tags ():
135- mode = request .args .get ('mode' )
136- is_debug = bool (request .args .get ('debug' ))
137- result = try_call (lib .get_scalar_tags , log_reader )
138- result = gen_result (0 , "" , result )
148+ data = cache_get ("/data/plugin/scalars/tags" , try_call ,
149+ lib .get_scalar_tags , log_reader )
150+ result = gen_result (0 , "" , data )
139151 return Response (json .dumps (result ), mimetype = 'application/json' )
140152
141153
142154@app .route ("/data/plugin/images/tags" )
143155def image_tags ():
144- mode = request . args . get ( 'run' )
145- result = try_call ( lib . get_image_tags , log_reader )
146- result = gen_result (0 , "" , result )
156+ data = cache_get ( "/data/plugin/images/tags" , try_call , lib . get_image_tags ,
157+ log_reader )
158+ result = gen_result (0 , "" , data )
147159 return Response (json .dumps (result ), mimetype = 'application/json' )
148160
149161
150162@app .route ("/data/plugin/histograms/tags" )
151163def histogram_tags ():
152- mode = request .args .get ('run' )
153- # hack to avlid IO conflicts
154- result = try_call (lib .get_histogram_tags , log_reader )
155- result = gen_result (0 , "" , result )
164+ data = cache_get ("/data/plugin/histograms/tags" , try_call ,
165+ lib .get_histogram_tags , log_reader )
166+ result = gen_result (0 , "" , data )
156167 return Response (json .dumps (result ), mimetype = 'application/json' )
157168
158169
159170@app .route ('/data/plugin/scalars/scalars' )
160171def scalars ():
161172 run = request .args .get ('run' )
162173 tag = request .args .get ('tag' )
163- result = try_call (lib .get_scalar , log_reader , run , tag )
164- result = gen_result (0 , "" , result )
174+ key = os .path .join ('/data/plugin/scalars/scalars' , run , tag )
175+ data = cache_get (key , try_call , lib .get_scalar , log_reader , run , tag )
176+ result = gen_result (0 , "" , data )
165177 return Response (json .dumps (result ), mimetype = 'application/json' )
166178
167179
168180@app .route ('/data/plugin/images/images' )
169181def images ():
170182 mode = request .args .get ('run' )
171183 tag = request .args .get ('tag' )
184+ key = os .path .join ('/data/plugin/images/images' , mode , tag )
172185
173- result = try_call (lib .get_image_tag_steps , log_reader , mode , tag )
174- result = gen_result (0 , "" , result )
186+ data = cache_get (key , try_call , lib .get_image_tag_steps , log_reader , mode ,
187+ tag )
188+ result = gen_result (0 , "" , data )
175189
176190 return Response (json .dumps (result ), mimetype = 'application/json' )
177191
@@ -181,21 +195,23 @@ def individual_image():
181195 mode = request .args .get ('run' )
182196 tag = request .args .get ('tag' ) # include a index
183197 step_index = int (request .args .get ('index' )) # index of step
184- offset = 0
185198
186- imagefile = try_call (lib .get_invididual_image , log_reader , mode , tag ,
187- step_index )
199+ key = os .path .join ('/data/plugin/images/individualImage' , mode , tag ,
200+ str (step_index ))
201+ data = cache_get (key , try_call , lib .get_invididual_image , log_reader , mode ,
202+ tag , step_index )
188203 response = send_file (
189- imagefile , as_attachment = True , attachment_filename = 'img.png' )
204+ data , as_attachment = True , attachment_filename = 'img.png' )
190205 return response
191206
192207
193208@app .route ('/data/plugin/histograms/histograms' )
194209def histogram ():
195210 run = request .args .get ('run' )
196211 tag = request .args .get ('tag' )
197- result = try_call (lib .get_histogram , log_reader , run , tag )
198- result = gen_result (0 , "" , result )
212+ key = os .path .join ('/data/plugin/histograms/histograms' , run , tag )
213+ data = cache_get (key , try_call , lib .get_histogram , log_reader , run , tag )
214+ result = gen_result (0 , "" , data )
199215 return Response (json .dumps (result ), mimetype = 'application/json' )
200216
201217
0 commit comments