Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions qcodes/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def run_temp(self, **kwargs):
return self.run(background=False, quiet=True,
data_manager=False, location=False, **kwargs)

def run(self, background=True, use_threads=True, enqueue=False,
def run(self, background=True, use_threads=True,
quiet=False, data_manager=None, **kwargs):
'''
execute this loop
Expand All @@ -419,8 +419,6 @@ def run(self, background=True, use_threads=True, enqueue=False,
use_threads: (default True): whenever there are multiple `get` calls
back-to-back, execute them in separate threads so they run in
parallel (as long as they don't block each other)
enqueue: (default False): wait for a previous background sweep to
finish? If false, will raise an error if another sweep is running
quiet: (default False): set True to not print anything except errors
data_manager: a DataManager instance (omit to use default,
False to store locally)
Expand All @@ -446,11 +444,11 @@ def run(self, background=True, use_threads=True, enqueue=False,

prev_loop = get_bg()
if prev_loop:
if enqueue:
prev_loop.join() # wait until previous loop finishes
else:
raise RuntimeError(
'a loop is already running in the background')
if not quiet:
print('Waiting for the previous background Loop to finish...',
flush=True)
prev_loop.join()

if data_manager is False:
data_mode = DataMode.LOCAL
else:
Expand All @@ -461,6 +459,10 @@ def run(self, background=True, use_threads=True, enqueue=False,
self.set_common_attrs(data_set=data_set, use_threads=use_threads,
signal_queue=self.signal_queue)

if prev_loop and not quiet:
print('...done. Starting ' + (data_set.location or 'new loop'),
flush=True)

if background:
p = QcodesProcess(target=self._run_wrapper, name=MP_NAME)
p.is_sweep = True
Expand Down
24 changes: 20 additions & 4 deletions qcodes/tests/test_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,28 @@ def test_foreground_no_datamanager(self):
def test_enqueue(self):
c1 = self.gates.chan1
loop = Loop(c1[1:5:1], 0.01).each(c1)
loop.run(location=self.location, quiet=True)
data1 = loop.run(location=self.location, quiet=True)

# second running of the loop should be enqueued, blocks until
# the first one finishes.
# TODO: check what it prints?
data2 = loop.run(location=self.location2, quiet=True)

data1.sync()
data2.sync()
self.assertEqual(data1.chan1.tolist(), [1, 2, 3, 4])
for v in data2.chan1:
self.assertTrue(np.isnan(v))

with self.assertRaises(RuntimeError):
loop.run(location=self.location2, quiet=True)
loop.run(location=self.location2, quiet=True, enqueue=True)
loop.process.join()
data2.sync()
self.assertEqual(data2.chan1.tolist(), [1, 2, 3, 4])

# and while we're here, check that running a loop in the
# foreground *after* the background clears its .process
self.assertTrue(hasattr(loop, 'process'))
loop.run_temp()
self.assertFalse(hasattr(loop, 'process'))


def sleeper(t):
Expand Down