Skip to content

Commit 55eab24

Browse files
feature: Add end_task
Allow to programmatically do something after the loop is done. F.ex save the plot.
1 parent eba6e95 commit 55eab24

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

qcodes/loops.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def __init__(self, sweep_values, delay=0, station=None,
180180
self.actions = None
181181
self.then_actions = ()
182182
self.bg_task = None
183+
self.end_task = None
183184
self.bg_min_delay = None
184185
self.progress_interval = progress_interval
185186

@@ -249,22 +250,25 @@ def each(self, *actions):
249250
return ActiveLoop(self.sweep_values, self.delay, *actions,
250251
then_actions=self.then_actions, station=self.station,
251252
progress_interval=self.progress_interval,
252-
bg_task=self.bg_task, bg_min_delay=self.bg_min_delay)
253+
bg_task=self.bg_task, end_task=self.end_task, bg_min_delay=self.bg_min_delay)
253254

254-
def with_bg_task(self, task, min_delay=1):
255+
def with_bg_task(self, task, end_task=None, min_delay=1):
255256
"""
256257
Attaches a background task to this loop.
257258
258259
Args:
259260
task: A callable object with no parameters. This object will be
260261
invoked periodically during the measurement loop.
261262
263+
end_task: A callable object with no parameters. This object will be
264+
invoked at the end of the measurement loop.
265+
262266
min_delay (default 1): The minimum number of seconds to wait
263267
between task invocations. Note that the actual time between
264268
task invocations may be much longer than this, as the task is
265269
only run between passes through the loop.
266270
"""
267-
return _attach_bg_task(self, task, min_delay)
271+
return _attach_bg_task(self, task, end_task, min_delay)
268272

269273
@staticmethod
270274
def validate_actions(*actions):
@@ -361,14 +365,17 @@ def _attach_then_actions(loop, actions, overwrite):
361365
return loop
362366

363367

364-
def _attach_bg_task(loop, task, min_delay):
368+
def _attach_bg_task(loop, task, end_task, min_delay):
365369
"""Inner code for both Loop and ActiveLoop.bg_task"""
366370
if loop.bg_task is None:
367371
loop.bg_task = task
368372
loop.bg_min_delay = min_delay
369373
else:
370374
raise RuntimeError('Only one background task is allowed per loop')
371375

376+
if end_task:
377+
loop.end_task = end_task
378+
372379
return loop
373380

374381

@@ -390,7 +397,7 @@ class ActiveLoop(Metadatable):
390397

391398
def __init__(self, sweep_values, delay, *actions, then_actions=(),
392399
station=None, progress_interval=None, bg_task=None,
393-
bg_min_delay=None):
400+
end_task=None, bg_min_delay=None):
394401
super().__init__()
395402
self.sweep_values = sweep_values
396403
self.delay = delay
@@ -399,6 +406,7 @@ def __init__(self, sweep_values, delay, *actions, then_actions=(),
399406
self.then_actions = then_actions
400407
self.station = station
401408
self.bg_task = bg_task
409+
self.end_task = end_task
402410
self.bg_min_delay = bg_min_delay
403411
self.data_set = None
404412

@@ -441,20 +449,23 @@ def then(self, *actions, overwrite=False):
441449
then_actions=self.then_actions, station=self.station)
442450
return _attach_then_actions(loop, actions, overwrite)
443451

444-
def with_bg_task(self, task, min_delay=1):
452+
def with_bg_task(self, task, end_task=None, min_delay=1):
445453
"""
446454
Attaches a background task to this loop.
447455
448456
Args:
449457
task: A callable object with no parameters. This object will be
450458
invoked periodically during the measurement loop.
451459
460+
end_task: A callable object with no parameters. This object will be
461+
invoked at the end of the measurement loop.
462+
452463
min_delay (default 1): The minimum number of seconds to wait
453464
between task invocations. Note that the actual time between
454465
task invocations may be much longer than this, as the task is
455466
only run between passes through the loop.
456467
"""
457-
return _attach_bg_task(self, task, min_delay)
468+
return _attach_bg_task(self, task, end_task, min_delay)
458469

459470
def snapshot_base(self, update=False):
460471
"""Snapshot of this ActiveLoop's definition."""
@@ -981,6 +992,12 @@ def _run_loop(self, first_delay=0, action_indices=(),
981992
for f in self._compile_actions(self.then_actions, ()):
982993
f()
983994

995+
# run the end_task from the bg_task:
996+
if self.end_task is not None:
997+
self.end_task()
998+
999+
1000+
9841001
def _wait(self, delay):
9851002
if delay:
9861003
finish_clock = time.perf_counter() + delay

0 commit comments

Comments
 (0)