@@ -144,36 +144,40 @@ def memray_workers(
144144 # Sleep for a brief moment such that we get
145145 # a clear profiling signal when everything starts
146146 time .sleep (0.1 )
147- yield
148- directory .mkdir (exist_ok = True )
149-
150- client = get_client ()
151- if fetch_reports_parallel is True :
152- fetch_parallel = len (workers )
153- elif fetch_reports_parallel is False :
154- fetch_parallel = 1
155- else :
156- fetch_parallel = fetch_reports_parallel
157-
158- for w in partition (fetch_parallel , workers ):
159- try :
160- profiles = client .run (
161- _fetch_memray_profile ,
162- filename = filename ,
163- report_args = report_args ,
164- workers = w ,
165- )
166- for worker_addr , profile in profiles .items ():
167- path = directory / quote (str (worker_names [worker_addr ]), safe = "" )
168- if report_args :
169- suffix = ".html"
170- else :
171- suffix = ".memray"
172- with open (str (path ) + suffix , "wb" ) as fd :
173- fd .write (profile )
174-
175- except Exception :
176- logger .exception ("Exception during report downloading from worker %s" , w )
147+ try :
148+ yield
149+ finally :
150+ directory .mkdir (exist_ok = True )
151+
152+ client = get_client ()
153+ if fetch_reports_parallel is True :
154+ fetch_parallel = len (workers )
155+ elif fetch_reports_parallel is False :
156+ fetch_parallel = 1
157+ else :
158+ fetch_parallel = fetch_reports_parallel
159+
160+ for w in partition (fetch_parallel , workers ):
161+ try :
162+ profiles = client .run (
163+ _fetch_memray_profile ,
164+ filename = filename ,
165+ report_args = report_args ,
166+ workers = w ,
167+ )
168+ for worker_addr , profile in profiles .items ():
169+ path = directory / quote (str (worker_names [worker_addr ]), safe = "" )
170+ if report_args :
171+ suffix = ".html"
172+ else :
173+ suffix = ".memray"
174+ with open (str (path ) + suffix , "wb" ) as fd :
175+ fd .write (profile )
176+
177+ except Exception :
178+ logger .exception (
179+ "Exception during report downloading from worker %s" , w
180+ )
177181
178182
179183@contextlib .contextmanager
@@ -226,20 +230,22 @@ def memray_scheduler(
226230 # Sleep for a brief moment such that we get
227231 # a clear profiling signal when everything starts
228232 time .sleep (0.1 )
229- yield
230- directory .mkdir (exist_ok = True )
231-
232- client = get_client ()
233-
234- profile = client .run_on_scheduler (
235- _fetch_memray_profile ,
236- filename = filename ,
237- report_args = report_args ,
238- )
239- path = directory / "scheduler"
240- if report_args :
241- suffix = ".html"
242- else :
243- suffix = ".memray"
244- with open (str (path ) + suffix , "wb" ) as fd :
245- fd .write (profile )
233+ try :
234+ yield
235+ finally :
236+ directory .mkdir (exist_ok = True )
237+
238+ client = get_client ()
239+
240+ profile = client .run_on_scheduler (
241+ _fetch_memray_profile ,
242+ filename = filename ,
243+ report_args = report_args ,
244+ )
245+ path = directory / "scheduler"
246+ if report_args :
247+ suffix = ".html"
248+ else :
249+ suffix = ".memray"
250+ with open (str (path ) + suffix , "wb" ) as fd :
251+ fd .write (profile )
0 commit comments