@@ -206,10 +206,12 @@ def _init_resources_file(task_name: str) -> Path:
206206
207207
208208def _log_resources (
209- file_path : Path , start_event : threading .Event , stop_event : threading .Event
209+ file_path : Path ,
210+ start_event : threading .Event ,
211+ stop_event : threading .Event ,
212+ sampling_interval : float ,
210213) -> None :
211214 max_duration = 0.0
212- statistics_interval = 1
213215 with open (file_path , "w" ) as stats_file :
214216 while not stop_event .is_set ():
215217 start_time = time .time ()
@@ -218,9 +220,9 @@ def _log_resources(
218220 end_time = time .time ()
219221 duration = end_time - start_time
220222 max_duration = duration if duration > max_duration else max_duration
221- # wait until next interval
222- if duration < statistics_interval :
223- time .sleep (statistics_interval - duration )
223+ # wait until next sampling interval
224+ if duration < sampling_interval :
225+ time .sleep (sampling_interval - duration )
224226 write_statistics (stats_file , close = True )
225227
226228
@@ -229,6 +231,7 @@ def track_resources(
229231 task_name : str ,
230232 start_event : threading .Event | None = None ,
231233 stop_event : threading .Event | None = None ,
234+ sampling_interval : float = 1.0 ,
232235) -> Iterator [None ]:
233236 """Monitor and track the resource usage of the host while executing a task context.
234237
@@ -240,13 +243,16 @@ def track_resources(
240243 task_name: The name of the task (used for naming the resources file).
241244 start_event: Controls the actual start of the resource tracking (optional).
242245 stop_event: Controls the actual end of the resource tracking (optional).
246+ sampling_interval: The measurement sampling interval in seconds (default: 1.0).
243247 """
244248 file_path = _init_resources_file (task_name )
245249 if start_event is None :
246250 start_event = threading .Event ()
247251 start_event .set ()
248252 stop_event = stop_event or threading .Event ()
249- thread = threading .Thread (target = _log_resources , args = (file_path , start_event , stop_event ))
253+ thread = threading .Thread (
254+ target = _log_resources , args = (file_path , start_event , stop_event , sampling_interval )
255+ )
250256 thread .start ()
251257
252258 yield
0 commit comments